Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 1.02 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to change the base class variables with the derived class object-c  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class A
  7. {
  8. public:
  9.   int a,b,c;
  10.   A()
  11.   {
  12.       a=0;
  13.       b=0;
  14.       c=0;
  15.   }
  16. };
  17.  
  18. class B:public A
  19. {
  20. public:
  21.     void get()
  22.     {
  23.         A *a2 =new A;
  24.         a2->a=10;
  25.         a2->b=20;
  26.         a2->c=30;
  27.         cout<<a2->a<<""<<a2->b<<""<<a2->c<<""<<endl;
  28.         cout<<"Checking!"<<endl;
  29.     }
  30. };
  31.  
  32. int main()
  33. {
  34.     A *a1 = new A;
  35.     B *b1 = new B;
  36.     cout<<a1->a<<""<<a1->b<<""<<a1->c<<""<<endl;
  37.     b1->a=10;
  38.     b1->b=20;
  39.     b1->c=30;
  40.     cout<<b1->a<<""<<b1->b<<""<<b1->c<<""<<endl;
  41.  
  42.     b1->get();//cant able to change the variables of the base class object with the derived class object
  43.     cout<<a1->a<<""<<a1->b<<""<<a1->c<<""<<endl;//will print the same values..
  44.  
  45.     //b1->get();
  46.  
  47.     return 0;
  48. }
  49.        
  50. get()
  51.        
  52. void get()
  53. {
  54.     a=10;
  55.     b=20;
  56.     c=30;
  57.     cout<<a<<b<<c<<endl;
  58.     cout<<"Checking!"<<endl;
  59. }
  60.        
  61. cout << a << b << c << "n";
  62.        
  63. cout << this->a << this->b << this->c << "n";