Наследование пример ООП C++

Наследование пример объектно-ориентированное программирование C++

#include <iostream>
using namespace std;

class A {
protected:
    unsigned int counter;
public:
    A() :counter(0) {};
    A(int a) {
        counter = a;
    };
    unsigned int get_counter() {
        return counter;
    };
    A operator++() {
        return A(++counter);
    };
    int get_two() {
        counter = counter * 2;
        return counter;
    }
    void show() const {
        cout << counter << endl;
    };
};

class B :public A {
public:
    B() :A() {};
    B(int x) :A() {};
    //B(int x) { counter = x; };
    int get_k() { 
        return A::get_two(); 
    };
    void message() const {
        A::show();
    }
    A operator--() {
        return A(--counter);
    }

};

void main()
{
    setlocale(LC_ALL, "Rus");
    //int x = 2;
    B obj;
    cout<<obj.get_counter()<<endl;
    ++obj;
    ++obj;
    ++obj;
    cout << obj.get_counter() << endl;
    --obj;
    cout << obj.get_counter() << endl;
    cout << obj.get_two() << endl;
    obj.show();
    obj.A::show();

    system("pause");
}

Вывод программы C++
наследование C++

 

513

Leave a Reply

Ваш адрес email не будет опубликован.