123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- class Human
- {
- protected:
- string name;
- int age;
- public:
- Human(string n, int a) : name(n), age(a)
- {
- cout << "ctor Human" << endl;
- }
- string getName() const {
- return name;
- }
- int gatAge() const {
- return age;
- }
- };
- class Employee : public Human
- {
- friend void applyTax(Employee &employee);
- private:
- double wage;
- public:
- Employee(string n, int a, double w) : wage(w), Human(n, a)
- {
- cout << "ctor Employee" << endl;
- }
- double getWage() const
- {
- return wage;
- }
- void print() const
- {
- cout << name << " " << age << " " << wage << endl;
- }
- };
- // Дружественная функция для доступа к приватным членам
- void applyTax(Employee &employee)
- {
- employee.wage *= 0.87;
- }
- // Приватный тип наследования. Можно записать как: class pEmployee : private Human
- class pEmployee : private Human
- {
- private:
- double wage;
- public:
- pEmployee(string n, int a, double w) : wage(w), Human(n, a)
- {
- cout << "ctor Employee" << endl;
- }
- };
- // --------------------------------------------------------------------------------- //
- class Shape
- {
- public:
- virtual void show() = 0;
- };
- class Circle : public Shape
- {
- public:
- void show() {
- cout << "Circle" << endl;
- }
- };
- class Triangle : Shape {
- public:
- void show() {
- cout << "Triangle" << endl;
- }
- };
- // --------------------------------------------------------------------------------- //
- class D {
- public:
- D() {cout << "D";}
- virtual ~D() {cout << "~D";}
- };
- class A : public D {
- public:
- A() {cout << "A";}
- ~A() {cout << "~A";}
- };
- class C : public D {
- public:
- C() {cout << "C";}
- virtual ~C() {cout << "~C";}
- };
- class B : public A,C {
- public:
- B() {cout << "B";}
- ~B() {cout << "~B";}
- };
- // --------------------------------------------------------------------------------- //
- enum sex_t {male, female};
- class Person {
- private:
- string name;
- int age;
- sex_t sex;
- float weight;
- public:
- Person(string n, int a, sex_t s, float w) : name(n), age(a), sex(s), weight(w) {}
-
- void setName(string n) {
- name = n;
- }
- };
- class Student : public Person {
- };
- int main()
- {
- Employee worker("Ivan", 20, 100000);
- applyTax(worker);
- worker.print();
- // pEmployee worker2("Sergey", 20, 120000);
- // worker2.print();
- Shape* shape1;
- shape1 = new Circle();
- shape1->show();
- A* b = new B();
- delete b;
-
- return 0;
- }
|