pacman 467 Опубликовано 24 апреля, 2015 Share Опубликовано 24 апреля, 2015 Помогите другу моему решить контрольную работу по программированию Цитата Ссылка на сообщение Поделиться на другие сайты
vit9696 415 Опубликовано 24 апреля, 2015 Share Опубликовано 24 апреля, 2015 (изменено) Ладно, сделаю тебе доброе дело. c++11: 1. #include <iostream> struct Base { Base() { } ~Base() { } char base_func(int a, int { return (char)((a + *(a + ); } }; int main() { auto *b = new Base; std::cout << b->base_func(1, 2) << std::endl; delete b; }2. #include <iostream> struct Base { Base() { } ~Base() { } char base_func(int a, int { return (char)((a + *(a + ); } }; class Derived : public Base { int a, b; public: Derived() = default; Derived(int a, int { this->a = a; this->b = b; } ~Derived() {} void put(int a, int { this->a = a; this->b = b; } void get(int &a, int { a = this->a; b = this->b; } int derived_func() { return base_func(a, ; } }; int main() { auto *d = new Derived(1, 2); std::cout << d->derived_func() << std::endl; delete d; }3. #include <iostream> template <typename T> struct Base { Base() { } ~Base() { } char base_func(T a, T { return (char)((a + *(a + ); } }; template <typename T> class Derived : public Base<T> { T a, b; public: Derived() = default; Derived(T a, T { this->a = a; this->b = b; } ~Derived() {} void put(T a, T { this->a = a; this->b = b; } void get(T &a, T { a = this->a; b = this->b; } T derived_func() { return this->base_func(a, ; } }; int main() { auto *d = new Derived<int>(1, 2); std::cout << d->derived_func() << std::endl; delete d; }4. #include <iostream> class Base { protected: Base() { } public: virtual ~Base() { } virtual char base_func(int a, int { return (char)((a + *(a + ); } virtual int derived_func() = 0; }; class Derived : public Base { int a, b; public: Derived() = default; Derived(int a, int { this->a = a; this->b = b; } ~Derived() {} void put(int a, int { this->a = a; this->b = b; } void get(int &a, int { a = this->a; b = this->b; } virtual int derived_func() { return base_func(a, ; } }; int main() { Base *d = new Derived(1, 2); std::cout << d->derived_func() << std::endl; delete d; } c++03: 1. #include <iostream> struct Base { Base() { } ~Base() { } char base_func(int a, int { return (char)((a + *(a + ); } }; int main() { Base *b = new Base; std::cout << b->base_func(1, 2) << std::endl; delete b; return 0; }2. #include <iostream> struct Base { Base() { } ~Base() { } char base_func(int a, int { return (char)((a + *(a + ); } }; class Derived : public Base { int a, b; public: Derived() {}; Derived(int a, int { this->a = a; this->b = b; } ~Derived() {} void put(int a, int { this->a = a; this->b = b; } void get(int &a, int { a = this->a; b = this->b; } int derived_func() { return base_func(a, ; } }; int main() { Derived *d = new Derived(1, 2); std::cout << d->derived_func() << std::endl; delete d; return 0; }3. #include <iostream> template <typename T> struct Base { Base() { } ~Base() { } char base_func(T a, T { return (char)((a + *(a + ); } }; template <typename T> class Derived : public Base<T> { T a, b; public: Derived() { }; Derived(T a, T { this->a = a; this->b = b; } ~Derived() {} void put(T a, T { this->a = a; this->b = b; } void get(T &a, T { a = this->a; b = this->b; } T derived_func() { return this->base_func(a, ; } }; int main() { Derived<int> *d = new Derived<int>(1, 2); std::cout << d->derived_func() << std::endl; delete d; return 0; }4. #include <iostream> class Base { protected: Base() { } public: virtual ~Base() { } virtual char base_func(int a, int { return (char)((a + *(a + ); } virtual int derived_func() = 0; }; class Derived : public Base { int a, b; public: Derived() { }; Derived(int a, int { this->a = a; this->b = b; } ~Derived() {} void put(int a, int { this->a = a; this->b = b; } void get(int &a, int { a = this->a; b = this->b; } virtual int derived_func() { return base_func(a, ; } }; int main() { Base *d = new Derived(1, 2); std::cout << d->derived_func() << std::endl; delete d; return 0; } P. S. Не по программированию, а по C++ T_T. Учись хоть тему озаглавливать правильно. Изменено 24 апреля, 2015 пользователем vit9696 1 1 Цитата Ссылка на сообщение Поделиться на другие сайты
Рекомендуемые сообщения
Присоединяйтесь к обсуждению
Вы можете написать сейчас и зарегистрироваться позже. Если у вас есть аккаунт, авторизуйтесь, чтобы опубликовать от имени своего аккаунта.