- #1
yungman
- 5,738
- 290
I am reviewing Inheritance again as Ivor book gets into exception using class object of base and derived class. I gone through a few books and still don't quite understand. This is the program I wrote to ask the questions:
1) I want to verify when saying the object of derived class inheriting member variables and member functions, this means the object of derived class has it's OWN member variables a, b and c. Has it's OWN member function BaseFunc21(), BaseFunc12() and DerivedFunc11().
2) The second question is more confusing to me as shown with BaseClass2 and DerivedClass2:
Line 33 shows DerivedClass2(int b, int c) : BaseClass2(a, b) { }. Notice variable b is common name defined in DerivedClass2 and in BaseClass2. How many b do I have in the object of derived class? b belongs to DerivedClass2 or BaseClass2?
For default constructor, it's easy to understand. Line 13 DerivedClass1() : BaseClass1(){ c = 0;} just simply combine both constructors to give a=0, b=0 and c=0. But for constructor with arguments and variable with the same name, it get's confusing. which one should I choose.
For member function with the same name, you can declare virtual in the base member function and the program will know if it's the derived class calls the function, it will just go to the function of the derived class.
thanks
C++:
#include<iostream>
using namespace std;
class BaseClass1
{public:
int a, b;
BaseClass1() { a = 0; b = 0; }
void BaseFunc11() {}
void BaseFunc21() {}
};
class DerivedClass1 : public BaseClass1
{public:
int c;
DerivedClass1() : BaseClass1(){ c = 0;}
void DerivedFunc11(){}
};
//I want to confirm that
//1) OBJECT of DerivedClass has it's own member variables a, b c.
//2) OBJECT of DerivedClass has it's own BaseFunc21(), BaseFunc12()
// and DerivedFunc11().class BaseClass2
{public:
int a, b;
BaseClass2() { a = 0; b = 0; }
BaseClass2(int A, int B) { a = A, b = B; }
void BaseFunc21() {}
void BaseFunc22() {}
};
class DerivedClass2 : public BaseClass2
{public:
DerivedClass2() : BaseClass2(a, b) { }
DerivedClass2(int b, int c) : BaseClass2(a, b) { }//How many b do we have?
void DerivedFunc21(){}
};
//Object of DerivedClass2 has member variable a, b and c
//Object of DerivedClass2 has member function BaseFunc21,
//BaseFunc22 and DerivedFunc21.
//b is being declared in DerivedClass2(int b, int c) AND BaseClass2(a, b)
//What does this mean? how many member variable b the DerivedClass2 object has?
int main()
{
DerivedClass1 DC1;
DerivedClass2 DC2;
}
1) I want to verify when saying the object of derived class inheriting member variables and member functions, this means the object of derived class has it's OWN member variables a, b and c. Has it's OWN member function BaseFunc21(), BaseFunc12() and DerivedFunc11().
2) The second question is more confusing to me as shown with BaseClass2 and DerivedClass2:
Line 33 shows DerivedClass2(int b, int c) : BaseClass2(a, b) { }. Notice variable b is common name defined in DerivedClass2 and in BaseClass2. How many b do I have in the object of derived class? b belongs to DerivedClass2 or BaseClass2?
For default constructor, it's easy to understand. Line 13 DerivedClass1() : BaseClass1(){ c = 0;} just simply combine both constructors to give a=0, b=0 and c=0. But for constructor with arguments and variable with the same name, it get's confusing. which one should I choose.
For member function with the same name, you can declare virtual in the base member function and the program will know if it's the derived class calls the function, it will just go to the function of the derived class.
thanks