Embedded Technocrats
|
|
Activity:
Question posted: 05 26 2008 00:05:28 +0000,
1 answers, 715 views, last activity
07 06 2010 20:18:08 +0000
|
|
|
|
When we inherit a class using private key word all functions in parent can be called by the child but not childs object in c++
see following example//Listing 12.3 Constructors and destructors called.
#include <iostream>
using namespace std;
enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
class Mammal
{
public:
// constructors
Mammal();
~Mammal();
//accessors
int GetAge() const { return itsAge; }
void SetAge(int age) { itsAge = age; }
int GetWeight() const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
//Other methods
void Speak() const { cout << "Mammal sound!\n"; }
void Sleep() const { cout << "shhh. I’m sleeping.\n"; }
protected:
int itsAge;
int itsWeight;
};
class Dog : private Mammal
{
public:
// Constructors
Dog();
~Dog();
// Accessors
BREED GetBreed() const { return itsBreed; }
void SetBreed(BREED breed) { itsBreed = breed; }
// Other methods
void WagTail() const { cout << "Tail wagging...\n"; }
void BegForFood() const { cout << "Begging for food...\n"; }
void Speak2()const {Speak();} // Parent class object is accessible
private:
BREED itsBreed;
};
Mammal::Mammal():
itsAge(3),
itsWeight(5)
{
std::cout << "Mammal constructor... " << endl;
}
Mammal::~Mammal()
{
std::cout << "Mammal destructor... " << endl;
}
Dog::Dog():
itsBreed(GOLDEN)
{
std::cout << "Dog constructor... " << endl;
}
Dog::~Dog()
{
std::cout << "Dog destructor... " << endl;
}
int main()
{
Dog Fido;
Fido.Speak2();
Fido.WagTail();
// std::cout << "Fido is " << Fido.GetAge() << " years old" << endl; //this will give compilation error because GetAge is parents function can not be called by child object
return 0;
}

|
|
|
|
|
|
|
|
|
|
|
|
AS BOTH VLSI EMBEDDED ARE CHIP DESIGN LEVEL PROGRAMMING WHICH ONE IS BETTER? MEANS WHICH HAVING BRIGHT FUTURE? |
When you inherit a class using private keyword which members of base class are visible to the derived class? |
How do you effectively desk check C code for an embedded software application? |