
Untitled
By: a guest on
Jun 20th, 2012 | syntax:
None | size: 1.02 KB | hits: 8 | expires: Never
How to change the base class variables with the derived class object-c
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
int a,b,c;
A()
{
a=0;
b=0;
c=0;
}
};
class B:public A
{
public:
void get()
{
A *a2 =new A;
a2->a=10;
a2->b=20;
a2->c=30;
cout<<a2->a<<""<<a2->b<<""<<a2->c<<""<<endl;
cout<<"Checking!"<<endl;
}
};
int main()
{
A *a1 = new A;
B *b1 = new B;
cout<<a1->a<<""<<a1->b<<""<<a1->c<<""<<endl;
b1->a=10;
b1->b=20;
b1->c=30;
cout<<b1->a<<""<<b1->b<<""<<b1->c<<""<<endl;
b1->get();//cant able to change the variables of the base class object with the derived class object
cout<<a1->a<<""<<a1->b<<""<<a1->c<<""<<endl;//will print the same values..
//b1->get();
return 0;
}
get()
void get()
{
a=10;
b=20;
c=30;
cout<<a<<b<<c<<endl;
cout<<"Checking!"<<endl;
}
cout << a << b << c << "n";
cout << this->a << this->b << this->c << "n";