Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Zadach C++
- //
- // Created by Хмелев Саша on 20/05/2017.
- // Copyright © 2017 Хмелев Саша. All rights reserved.
- //
- /* Реализовать указанную схему наследования. В базовых классах реализовать функцию print(), которая выводит на консоль имя класса, из которого она вызвана. Написать функцию show(), которая выводит все доступные для этого класса переменные. */
- #include <iostream>
- using namespace std;
- class A1 { // базовый класc
- protected:
- int a;
- public:
- A1() {
- a = 1;
- }
- void print() {
- cout << "A1";
- }
- void show() {
- cout << a;
- }
- };
- class B1 : virtual public A1 {
- protected:
- int b1;
- public:
- B1() : A1() {
- b1 = 2;
- }
- void print() {
- cout << "B1";
- }
- void show() {
- cout << b1 << a;
- }
- };
- class B2 : virtual public A1 {
- protected:
- int b2;
- public:
- B2() : A1() {
- b2 = 3;
- }
- void print() {
- cout << "B2";
- }
- void show() {
- cout << b2 << a;
- }
- };
- class C1 : public B1, public B2 {
- protected:
- int c;
- public:
- C1() : B1(), B2() {
- c = 4;
- }
- void print() {
- cout << "C1";
- }
- void show() {
- cout << c << b2 << b1 << a;
- }
- };
- int main() {
- A1 a;
- a.print();
- cout << endl;
- A1 a11;
- a11.show();
- cout << endl;
- cout << endl;
- B1 b1;
- b1.print();
- cout << endl;
- B1 b11;
- b11.show();
- cout << endl;
- cout << endl;
- B2 b2;
- b2.print();
- cout << endl;
- B2 b21;
- b21.show();
- cout << endl;
- cout << endl;
- C1 c;
- c.print();
- cout << endl;
- C1 c11;
- c11.show();
- cout << endl;
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement