faqts : Computers : Programming : Languages : C++

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

11 of 18 people (61%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

C++: Class: Inheritance: Create: Simple: Can you give an example of inheritance?

Sep 30th, 2003 05:54
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 27 September 2003 - 07:10 pm ------------------

C++: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?

1. In general:

Steps: Overview:

 1. -Create a class <yourclassnameparent>:

http://www.faqts.com/knowledge_base/view.phtml/aid/24828/fid/163

 2. -Create another class <yourclassnamechild>:

     1. To indicate that you want to use inheritance, use the following
        notation:

 class <yourclassnamechild> : <access modifier> <yourclassnameparent> {
  // do something
  // e.g. -add new variables, functions and procedures
  //
  //      and or
  //
  //      -overwrite existing variables, functions
  //       and procedures of that 'parent' class
 };

     2. So you indicate that your class is inheriting, by writing

        1. first the word 'class'

        2. followed by the name of 'child' class

        3. followed by a colon ':'

        4. followed by an access modifier
           (like 'private', 'protected', 'public')

        5. followed by the name of the 'parent' class


http://www.faqts.com/knowledge_base/view.phtml/aid/24762/fid/163

---
---

2. In specific:

 1. -Create a parent class 'addressC':

      class addressC {
       //
       protected:
       //
       string nameS;
       string streetS;
       string cityS;
       string countryS;
       string telephoneS;
       string emailS;
       //
       public:
       void PROCView();
       //
       addressC( string nameS, string streetS, string cityS, string 
countryS, string telephoneS, string emailS ); // constructor for 
parent class
       //
       ~addressC(); // destructor for parent class
      };

 2. -Create another child class 'addresscompanyC':

      class addresscompanyC : public addressC {
      //
      private:
      //
      string companynameS;
      //
      public:
      //
      void PROCView();
      //
      addresscompanyC( string nameS, string streetS, string cityS, 
string countryS, string telephoneS, string emailS, string 
companynameS );
      //
      ~addresscompanyC(); // destructor
      };

 3. -Then give the code for the procedures or functions used in that 
class:

     1. For the parent class:

         addressC::addressC( string nameS, string streetS, string 
cityS, string countryS, string telephoneS, string emailS ) {
          this->nameS = nameS;
          this->streetS = streetS;
          this->cityS = cityS;
          this->countryS = countryS;
          this->telephoneS = telephoneS;
          this->emailS = emailS;
         };

http://www.faqts.com/knowledge_base/view.phtml/aid/16106

         addressC::~addressC() {
         };

         void addressC::PROCView() {
          cout << nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' + ' ' 
+ cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + telephoneS 
+ ' ' + '-' + ' ' + emailS + "\n";
         };

     2. For the child class:

         addresscompanyC::addresscompanyC( string nameS, string 
streetS, string cityS, string countryS, string telephoneS, string 
emailS, string companynameS ) : addressC( nameS, streetS, cityS, 
countryS, telephoneS, emailS ) {
          this->companynameS = companynameS;
         };

         void addresscompanyC::PROCView() {
          cout << nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' + ' ' 
+ cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + telephoneS 
+ ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' + companynameS + "\n";
         };

         addresscompanyC::~addresscompanyC() {
         };

 4. -Declare the variables which you intend to use with your class:

      addressC knudO( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
      addressC vanessaO( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
      addresscompanyC knudcompanyO( "myname", "mystreet 
1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
      addresscompanyC vanessacompanyO( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );

 5. -Supply some source code where you use your class:

      main() {
       // do something
       knudO.PROCView();
       knudcompanyO.PROCView();
       };

 6. -This gives alltogether the following source code:

      // save e.g. as 'testinheritance.cpp'

      #include <iostream.h>

        // --- 1. declaration of the 'parent' class --- //

        class addressC {
         //
         // it must be 'protected', because
         // the 'child' class must be able to read their values
         //
         protected:
         //
         string nameS;
         string streetS;
         string cityS;
         string countryS;
         string telephoneS;
         string emailS;
         //
         public:
         //
         // 1.1.1 a getter for the 'parent' class
         void PROCView();
         //
         // 1.1.2 the 'constructor' for the 'parent' class
         addressC( string nameS, string streetS, string cityS, string 
countryS, string telephoneS, string emailS );
         //
         // 1.1.3 the 'destructor' for the 'parent' class
         ~addressC();
        };

        // --- 2. declaration of the 'child' class --- //

        class addresscompanyC : public addressC {
        //
        private:
        //
        string companynameS;
        //
        public:
        //
        // 2.1.1 getter for child class
        void PROCView();
        //
        // 2.1.2 constructor for child class
        addresscompanyC( string nameS, string streetS, string cityS, 
string countryS, string telephoneS, string emailS, string 
companynameS ); // constructor for child class
        //
        // 2.1.3 destructor for child class
        ~addresscompanyC(); // destructor for child class
        };

        // --- 1.1 functions and procedures of the 'parent' class ---
 //

         // 1.1.1 a 'getter' of the 'parent' class
         void addressC::PROCView() {
          cout << nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' + ' ' 
+ cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + telephoneS 
+ ' ' + '-' + ' ' + emailS + "\n";
         };

         // 1.1.2 the 'constructor' of the 'parent' class
         addressC::addressC( string nameS, string streetS, string 
cityS, string countryS, string telephoneS, string emailS ) {
          this->nameS = nameS;
          this->streetS = streetS;
          this->cityS = cityS;
          this->countryS = countryS;
          this->telephoneS = telephoneS;
          this->emailS = emailS;
         };

         // 1.1.3 the 'destructor' of the 'parent' class
         addressC::~addressC() {
         };

        // --- 2.1 functions and procedures of the 'child' class --- //

         // 2.1.1 a 'getter' of the 'child' class
         void addresscompanyC::PROCView() {
          cout << nameS + ' ' + '-' + ' ' + streetS + ' ' + '-' + ' ' 
+ cityS + ' ' + '-' + ' ' + countryS + ' ' + '-' + ' ' + telephoneS 
+ ' ' + '-' + ' ' + emailS + ' ' + '-' + ' ' + companynameS + "\n";
         };

         // 2.1.2 the 'constructor' for the 'child' class (which calls 
the 'constructor' of the 'parent' class first)
         addresscompanyC::addresscompanyC( string nameS, string 
streetS, string cityS, string countryS, string telephoneS, string 
emailS, string companynameS ) : addressC( nameS, streetS, cityS, 
countryS, telephoneS, emailS ) {
          this->companynameS = companynameS;
         };

         // 2.1.3 the 'destructor' of the 'child' class
         addresscompanyC::~addresscompanyC() {
         };

        // --- 3. do something --- //

         main() {
          //
          // 3.1 create and initialize variables representing 
that 'parent' class:
          //
          addressC knudO( "myname", "mystreet 
1", "mycity", "mycountry", "01-2345678", "myemail@address" );
          addressC vanessaO( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address" );
          //
          // 3.2 create initialize variables representing that 'child' 
class:
          //
          addresscompanyC knudcompanyO( "myname", "mystreet 
1", "mycity", "mycountry", "01-
2345678", "myemail@address", "thecompany1name" );
          addresscompanyC vanessacompanyO
( "anothername", "anotherstreet 
1", "anothercity", "anothercountry", "01-
3456789", "anotheremail@address", "thecompany2name" );
          //
          // 3.3 do something
          //
          //  3.3.1 view variables stored in the variables 
representing that 'parent' class:
          //
          knudO.PROCView();
          vanessaO.PROCView();
          //
          //  3.3.2 view variables stored in the variables 
representing that 'child' class:
          //
          knudcompanyO.PROCView();
          vanessacompanyO.PROCView();
          };

 7. -When you run this source code, you will see e.g. a text screen
     with the following information:


> c:\borland\bcc55\bin\bcc32.exe
  -Ic:\borland\bcc55\include
  -Lc:\borland\bcc55\lib

   testinheritance.cpp

> testinheritance.exe

myname - mystreet 1 - mycity - mycountry - 01-2345678 - myemail@address

anothername - anotherstreet 1 - anothercity - anothercountry -
01-3456789 - anotheremail@address

myname - mystreet 1 - mycity - mycountry - 01-2345678 - myemail@address
- thecompany1name

anothername - anotherstreet 1 - anothercity - anothercountry -
01-3456789 - anotheremail@address - thecompany2name

Press any key to continue . . .

---
---

Internet: see also:

C#: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24889/fid/791

C++: Borland: Compiler: Command: Line: Install: How to install free 
Borland command line compiler?
http://www.faqts.com/knowledge_base/view.phtml/aid/24923/fid/163

C++: Class: Inheritance: What is inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24866

Delphi: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24880/fid/175

Java: Class: Inheritance: Create: Simple: Can you give an example of 
inheritance?
http://www.faqts.com/knowledge_base/view.phtml/aid/24891/fid/165

----------------------------------------------------------------------