learncpponline

Single Inheritance – Public Inheritance in C++

Inheritance in which the derived class is derived from only one base class is called single inheritance. Let us consider a simple example to illustrate the single inheritance. Program given below shows a base class B and a derived class D. The class B contains one private data member, one public data member and three public member functions. The class D contains one private data member and two public member functions. #include<iostream.h> class B { private: int a; //private; not inheritable public: int b; //public; ready for inheritance void get_ab(); int get_a(); void show_a(); }; class D : public B //public derivation { private: int c; public: void mul(); void display(); }; /*…Defining of functions…*/ void B :: get_ab() { a = 5; b = 10; } int B :: get_a() { return a; } void B :: show_a() { cout << “a = ” << a <<endl; } void...

Defining of Derived Classes in C++

How do we define a derived class in C++ A derived class is defined by specifying it relationship with the base class in addition to its own details. The general form of defining a derived class is: class derived-class-name : visibility-mode base-class-name { … … //members of derived class … }; where, class is the required keyword, derived-class-name is the name given to the derived class, base-class-name is the name given to the base class, : (colon) indicates that the derived-class-name is derived from the base-class-name, visibility-mode is optional and, if present, may be either private or public. The default visibility-mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived. Examples: class ABC : private XYZ //private derivation { members of ABC }; class ABC : public XYZ //public derivation { members of ABC }; class ABC : XYZ //private derivation...

Destructors in C++

A destructor in C++, as the name implies, is used to destroy the objects that have been created by a constructor. Like a constructor, the destructor in C++ is a member function whose name is the same as the class name but is preceded by a tilde (~). For example, the destructor for the class integer can be defined as shown below: ~integer() { } A destructor in C++ never takes any arguments nor does it return any value. It will be invoked implicitly by the compiler upon exit from the program (or the block or the function as the case may be) to clean up the storage that is no longer accessible. It is a good practice to declare destructors in a program since it releases memory space for future use. Whenever new is used to allocate memory in the constructors, we should use delete to free that memory....

Constructor with Default Arguments in C++

It is possible in C++ to define constructors with default arguments. For example, the constructor integer(), defined in the previous section (Constructors in C++), can be defined as follows: integer(int x, int y = 3); The default value of the argument y is 3. Thus, the statement: integer a(1); assigns the value 1 to x variable and 3 to y (by default). However, the statement: integer a(1, 5); assigns 1 to x and 5 to y. The actual parameters, when specified, overrides the default value. Note: The missing arguments must be the trailing ones.

Parameterized Constructors in C++

The constructor integer(), defined in the previous section (Constructors in C++), initializes the data members of all the objects to zero. However, in practice it may be necessary to initialize the various data elements of different objects with different values when the are created. C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created. The constructors that can take the arguments are called parameterized constructors. The constructor integer() may be modified to take arguments as shown below: class integer { private: int m, n; public: integer(int x, int y); //parameterized constructor … … }; integer :: integer(int x, int y) { m = x; n = y; } When a constructor has been parameterized, the object declaration statement such as: integer in1; may not work. We must pass the initial values as arguments to the constructor function when an object is...

Constructors in C++

A constructor in C++ is a special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class. A constructor is declared and defined as follows: class integer //class with a constructor { private: int m, n; public: integer(); //constructor declared … … }; integer :: integer() //constructor defined { m = 0; n = 0; } When a class contains a constructor like the one defined above, it is guaranteed that an object created by the class will be initialized automatically. For example, the declaration: integer in1; //object in1 created not only creates the object in1 of type integer but also initializes its data members m and...