Build your professional network on facebook via our app Go to app
 
 
Posted in Community :

Embedded Technocrats

 
Asked by : Siddharth Chakrabarty, Project Lead, Texas Instrument
Industry : Semiconductor
Functional Area : Programming Languages
Activity: Question posted: 05 26 2008 00:05:28 +0000, 1 answers, 715 views, last activity 07 06 2010 20:18:08 +0000
 
Share
 
 
  Rate : 
 
 
 Top Answer :
Rating : +1 

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;
}



[ Delete ]
[ Edit ]
Report Abuse
by   gelliphanindraviswanadhaprasad , Design Engineer, Mistral Software  | 08 12 2009 04:57:28 +0000
 
Leading recruitment Firm
Excellent communication in English, Associate/Senior Associates, www.delphicjobs.com, Bangalore
Excellent communication skills, Associate, 24/7 Customer, Bangalore
Viewers also viewed
Hi All, How is the idea of moving from .net Development to Java development sound to you? One of...
 
181 referals 22 arguments, 933 views
Education is nothing but giving knowledge to individuals, some grab it quickly some may take...
 
1897 referals 48 arguments, 4635 views
Power consumption increases in summer and then we see power crisis. Every time there is power...
 
510 referals 14 arguments, 482 views
more...  
Unanswered Questions (26)
I guess here I will find my answer. I have a website of web design and development. Its traffic...
 
0 referals 0 answers, 0 views
Well I love to do online shopping from core clothing .It is a great online shopping store for...
 
0 referals 0 answers, 0 views
The  ASYANA self tanning products tanning lotion gives your skin a natural tan just like the sun...
 
0 referals 0 answers, 0 views
More From Author
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?
more...