Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <string>
- using namespace std;
- //Wprowadzenie:
- class A {
- private:
- int x_A;
- protected:
- int y_A;
- public:
- int z_A;
- A() : x_A(0), y_A(0), z_A(0)
- {
- cout << "Lista inicjalizacyjna A zostala wywolana" << endl;
- }
- ~A()
- {
- cout << "Destruktor A zostal wywolany" << endl;
- }
- void print_values_A()
- {
- cout << x_A << "\t" << y_A << "\t" << z_A << endl;
- }
- };
- class B : public A
- {
- private:
- int x_B;
- protected:
- int y_B;
- public:
- int z_B;
- B(int x) : A(), x_B(x), y_B(0), z_B(0)
- {
- cout << "Lista inicjalizacyjna B zostala wywolana" << endl;
- }
- ~B()
- {
- cout << "Destruktor B zostal wywolany" << endl;
- }
- void print_values_B()
- {
- cout << y_A << "\t" << z_A << "\t" << x_B << "\t" << y_B << "\t" << z_B << endl;
- }
- };
- class C : protected B
- {
- private:
- int x_C;
- protected:
- int y_C;
- public:
- int z_C;
- C(int x) : B(x), x_C(x), y_C(0), z_C(1)
- {
- cout << "Konstruktor C zostal wywolany" << endl;
- }
- ~C()
- {
- cout << "Destruktor C zostal wywolany" << endl;
- }
- void print_values_C()
- {
- cout << y_B << "\t" << z_A << "\t" << y_B << "\t" << z_B << "\t" << x_C << "\t" << y_C << "\t" << z_C << endl;
- }
- };
- class AA
- {
- protected:
- double x, y;
- public:
- AA(double x, double y) : x(x), y(y) { cout << "Konstruktor AA zostal wywolany" << endl; }
- virtual double compute()
- {
- return x + y;
- }
- AA() {}
- virtual ~AA() { cout << "Wirtualny destruktor AA zostal wywolany" << endl; }
- };
- class BB : public AA
- {
- public:
- BB(double x, double y) : AA(x, y) { cout << "Konstruktor BB zostal wywolany" << endl; }
- virtual double compute()
- {
- return x * y;
- }
- BB() {}
- virtual ~BB() { cout << "Wirtualny destruktor BB zostal wywolany" << endl; }
- };
- //Zadania:
- class Shape
- {
- protected:
- string name;
- public:
- Shape(string n)
- {
- this->name = n;
- cout << "Konstruktor shape zostal wywolany" << endl;
- }
- Shape()
- {
- this->name = "";
- cout << "Konstruktor domyslny shape zostal wywolany" << endl;
- }
- //Shape(string name) : name(name) { cout << "Konstruktor shape zostal wywolany" << endl; }
- ~Shape() { cout << "Destruktor shape zostal wywolany" << endl; }
- void setName(string name)
- {
- this->name = name;
- }
- string getName()
- {
- return name;
- }
- virtual double area()
- {
- return 0.00;
- }
- };
- class Rectangle : public Shape
- {
- protected:
- double width;
- double height;
- public:
- Rectangle()
- {
- this->name = "";
- this->width = 0;
- this->height = 0;
- }
- Rectangle(string n, double w, double h)
- {
- this->name = n;
- this->width = w;
- this->height = h;
- }
- ~Rectangle() { cout << "Destruktor rectangle zostal wywolany" << endl; }
- double area()
- {
- return width * height;
- }
- };
- class Square : public Rectangle
- {
- public:
- Square(string n, double w)
- {
- cout << "Konstruktor square zostal wywolany" << endl;
- this->name = n;
- this->width = w;
- }
- virtual double area()
- {
- return width * width;
- }
- ~Square() { cout << "Destruktor square zostal wywolany" << endl; }
- };
- void Wprowadzenie()
- {
- //1
- /*A a;
- a.print_values_A();
- B b(5);
- b.print_values_B();
- C c(7);
- c.print_values_C();*/
- //2
- /*A a1;
- B b1(5);
- C c1(2);
- cout << "Zwykle A" << endl;
- a1.print_values_A();
- b1.print_values_A();
- b1.print_values_B();
- c1.print_values_C();
- A &a2 = b1;
- a2.print_values_A();
- A *a3 = &b1;
- cout << "Wskaznik A" << endl;
- a3->print_values_A();*/
- //3
- /*AA aa1(5, 4);
- cout << aa1.compute() << endl;
- BB bb1(5, 4);
- cout << bb1.compute() << endl;
- AA& aa2_bb1 = bb1;
- cout << aa2_bb1.compute() << endl;
- AA* aa3_bb1 = &bb1;
- cout << aa3_bb1->compute() << endl;
- BB* pbb = new BB(3,1);
- AA* paa_bb = new AA(3,1);
- cout << "Wskaznik do wirtualnej metody: \n" << pbb->compute() << endl;
- cout << paa_bb->compute() << endl;*/
- }
- void Zad1()
- {
- Shape shape("Kwadrat");
- cout << shape.getName() << endl;
- //shape.setName("Trojkat");
- Rectangle a("Prostokat",5,8);
- Square b1("Kwadrat", 9);
- Square *b2 = &b1;
- b2->setName("Kwadrat2");
- cout << b2->getName() << " : " << b2->area() << endl;
- }
- int main()
- {
- Wprowadzenie();
- Zad1();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement