Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Practice Notes: A Roadmap to Proficiency part 3, Study notes of Computer Science

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

2023/2024

Available from 12/17/2024

myraa
myraa 🇮🇳

6 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page | 1
Unit IV
1. Inheritance
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:
2. Defining Derived Classes
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
{
pf3
pf4
pf5
pf8

Partial preview of the text

Download C++ Practice Notes: A Roadmap to Proficiency part 3 and more Study notes Computer Science in PDF only on Docsity!

Unit IV

1. Inheritance

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:

2. Defining Derived Classes

■ 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

  • Public members of base class become private members of derived class.
  • Public members of the base class can only be accessed by member functions of derived class ■ When a base class is publicly inherited by a derived class
  • Public members of base class become public members of derived class.
  • They are accessible to the objects of the derived class

3. Single Inheritance

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

6. Questions

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++?

7. Hierarchical Inheritance

In hierarchical inheritance, the derived classes form a hierarchy. Syntax: class A { …………… }; class B : public A { ………….. }; class C : public A { …………….. };

8. Multiple Inheritance

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() {

A

B C

A B

C

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

9. Hybrid Inheritance

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 { ………….. };

10. Virtual Base Classes

■ 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(); }

12. Questions

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++?

13. Constructor in Derived Classes

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

14. Nesting of Classes

■ 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

15. Questions

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?