Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- using namespace std;
- class X
- {
- protected:
- int *x1, *x2;
- public:
- X(int, int);
- ~X();
- virtual void set(int, int);
- virtual void print();
- };
- X::X(int i, int j)
- {
- x1 = new int(i);
- x2 = new int(j);
- }
- X::~X()
- {
- delete x1;
- delete x2;
- }
- void X::set(int i, int j)
- {
- *x1 = i;
- *x2 = j;
- };
- void X::print()
- {
- cout<<"X1= "<<*x1<<endl<<"X2= "<<*x2<<endl;
- };
- class Y : public X
- {
- private:
- int *y;
- public:
- Y(int, int, int);
- ~Y();
- void set(int, int, int);
- void print();
- };
- Y::Y(int i, int j, int k):X(i, j)
- {
- y = new int(k);
- }
- Y::~Y()
- {
- delete y;
- }
- void Y::set(int i, int j, int k)
- {
- *x1 = i;
- *x2 = j;
- *y = k;
- }
- void Y::print()
- {
- cout<<"X1= "<<*x1<<endl<<"X2= "<<*x2<<endl<<"Y= "<<*y<<endl<<"Result= "<<*x1+*x2-*y<<endl;
- }
- void main()
- {
- Y obj1(1, 3, 5);
- Y obj2(2, 4, 6);
- X *p;
- p=&obj1;
- p->print();
- p=&obj2;
- p->print();
- obj2.set(7,8,9);
- p->print();
- Y obj(10, 5, 11);
- obj.print();
- obj.set(3,3,3);
- obj.print();
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement