




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
C++ practice notes cover essential programming concepts, including syntax, data types, control structures, functions, object-oriented programming, STL, memory management, and advanced topics like inheritance and polymorphism.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!
Inheritance is based on the concept of code reusability. It saves time of program development. The mechanism of deriving a new derived class (subclass) from an old base class is known as inheritance. Types of Inheritance:
■ A derived class can be defined by specifying its relationship with the base class in addition to its own details. class derived_class : visibility_mode base_class { ………… …………//members of derived class }; ■ Default visibility mode is private ■ Example 1 (Private Derivation): class ABC : private XYZ { members of ABC }; ■ Example 2 (Public Derivation): class ABC : public XYZ { members of ABC }; ■ Example 3 (Default private Derivation): class ABC : XYZ {
members of ABC }; ■ When a base class is privately inherited by a derived class
In single inheritance a single sub class is derived from the base class. Syntax 1: class A //Base Class { …………… }; class B: public A //Derived Class { ………….. }; Syntax 2: class A //Base Class { …………… }; class B: private A //Derived Class { ………….. }; Example of Single Inheritance: class base { public: int x; void getdata() { cout << "Enter the value of x = "; cin >> x; } }; class derive : public base { private: int y; public: void readdata() { cout << "Enter the value of y = "; cin >> y; } void product() { cout << "Product = " << x * y; } }; void main() { derive a; a.getdata(); a.readdata(); a.product(); } A B
Q.1) Define Inheritance. Q.2) How can you define derived classes from base classes using inheritance? Q.3) What is Single Inheritance? How can it be implemented in C++? Q.4) How can you make a private member inheritable? Q.5) What is Multi-level Inheritance? How can it be implemented in C++?
In hierarchical inheritance, the derived classes form a hierarchy. Syntax: class A { …………… }; class B : public A { ………….. }; class C : public A { …………….. };
In multiple inheritance a sub-class is derived from multiple base classes. Syntax: class A { …………… }; class B { ………….. }; class C : public A, public B { …………….. }; Example of Multiple Inheritance: class Vehicle { public: int cost; }; class Manufacturer { public: char m_name[20]; }; class Buyer: public Vehicle,public Manufacturer { public: char b_name[20]; }; void main() {
Buyer b; clrscr(); cout<<"\nEnter cost:"; cin>>b.cost; cout<<"\nEnter buyer name:"; cin>>b.b_name; cout<<"\nEnter manufacturer name:"; cin>>b.m_name; cout<<"Name of buyer:"<<b.b_name<<endl; cout << "Cost of Car=" << b.cost << endl; cout << "Manufacturer name= " << b.m_name<< endl; getch(); }
Multiple types of inheritance is combined in a hybrid inheritance. Syntax: class A { …………… }; class B : public A { ………….. }; class C : public A { …………….. }; class D : public B, public C { ………….. };
■ Virtual base class is used in situation where a derived class have multiple copies of base class. ■ Avoids duplication of inherited members A B C D
public: void sound() { cout<<“Meow"<<endl; } }; void main() { Dog obj; obj.sound(); obj.sleeping(); Cat obj1; obj1.sound(); obj1.sleeping(); }
Q.1) What is Hierarchical Inheritance? How can it be implemented in C++? Q.2) What is Multiple Inheritance? How can it be implemented in C++? Q.3) What is Hybrid Inheritance? How can it be implemented in C++? Q.4) What is a Virtual Base Class? Q.5) What are abstract classes? How can it be implemented in C++?
■ When a class is derived from another class, it is possible to define a constructor in the derived class, and the data members of both base and derived classes can be initialized. ■ It is not essential to declare a constructor in the base class ■ Example: class A { protected: int x,y; }; class B : private A { public: int z; B() { x=1; y=2;z=3; cout<<“x=“<<x<<“y=“<<y<<“z=“<<z; } }; void main() { B b; }
■ Nested class is a class defined inside a class, that can be used within the scope of the class in which it is defined. ■ Example: class Nest { public:
class Display { private: int s; public: void sum( int a, int b) { s =a+b; } void show( ) { cout << "\nSum of a and b is:: " << s;} }; }; void main() { Nest::Display x; x.sum(12, 10); x.show(); } Output: Sum of a and b is:: 22
Q.1) How can you use Constructor in Derived Classes? Q.2) What is nesting of classes? How can it be implemented in C++? Q.3) Write a C++ program to implement a constructor in a derived class. Q.4) What do you mean by a derived class? Q.5) How can a constructor be created for a derived class?