lab 3 oops
lab 3 oops
lab 3 oops
3
27/09/2024
INHERITANCE IN C++
Lab outcomes:
Theory:
Inheritance is one of the cornerstones of Object-Oriented Programming
(OOP) in C++. It allows a new class (derived class) to inherit properties and
behaviors from an existing class (base class). This mechanism enables code
reuse, extensibility, and the establishment of a relationship between classes,
facilitating the creation of more complex systems in a structured manner.
Below, we’ll dive deep into every aspect of inheritance, explaining how it
works and providing examples for each type of inheritance. In C++, we use
the : symbol to perform inheritance. For example,
What is Inheritance?
Inheritance in C++ allows you to create new classes based on existing
classes. This concept is central to OOP because it enables code reuse and
helps in maintaining and extending existing code without duplication. The
class that inherits properties is called the derived class, and the class from
which it inherits is known as the base class.
Purpose and Benefits of Inheritance
Code Reusability: By inheriting from an existing class, you avoid
rewriting common functionality, reducing code duplication.
Extensibility: You can extend existing classes with new features
without modifying the original class.
Abstraction: Inheritance promotes abstract thinking, allowing you to
design more general classes and create specific classes based on
them.
Polymorphism Support: It works closely with polymorphism to
enable dynamic binding of methods.
Syntax of Inheritance
The syntax for inheritance in C++ uses a colon (:) followed by an access
specifier (public, protected, or private) and the name of the base class:
class BaseClass {
// Members of the base class
};
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
int main() {
Dog dog;
dog.eat(); // Accessible due to public inheritance
dog.bark(); // Defined in Dog class
return 0;
}
5. Types of Inheritance
C++ supports several types of inheritance, each with its own characteristics.
Let's explore each type in detail with examples:
class Person {
public:
void speak() {
cout << "Person is speaking..." << endl;
}
};
int main() {
Student student;
student.speak(); // Inherited from Person
student.study(); // Defined in Student
return 0;
}
class Engine {
public:
void start() {
cout << "Engine starting..." << endl;
}
};
class Wheels {
public:
void roll() {
cout << "Wheels rolling..." << endl;
}
};
int main() {
Car car;
car.start(); // Inherited from Engine
car.roll(); // Inherited from Wheels
car.drive(); // Defined in Car
return 0;
}
class Base2 {
public:
void display() {
cout << "Display from Base2" << endl;
}
};
int main() {
Derived obj;
obj.show();
return 0;
}
5.3 Multilevel Inheritance
A class derives from another derived class, forming a chain of inheritance.
Example Code
#include <iostream>
using namespace std;
class LivingBeing {
public:
void breathe() {
cout << "Breathing..." << endl;
}
};
int main() {
Bird bird;
bird.breathe(); // Inherited from LivingBeing
bird.move(); // Inherited from Animal
bird.fly(); // Defined in Bird
return 0;
}
class Shape {
public:
void draw() {
cout << "Drawing shape..." << endl;
}
};
int main() {
Circle circle;
Square square;
circle.draw(); // Inherited from Shape
circle.drawCircle(); // Specific to Circle
int main() {
Bat bat;
bat.makeSound(); // No ambiguity due to virtual inheritance
return 0;
}
class Base {
public:
Base() {
cout << "Base class constructor called." << endl;
}
~Base() {
cout << "Base class destructor called." << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived class constructor called." << endl;
}
~Derived() {
cout << "Derived class destructor called." << endl;
}
};
int main() {
Derived obj;
return 0;
}
8. Summary
Inheritance in C++ is a powerful tool that supports the creation of complex
and reusable class hierarchies. Understanding its different forms (single,
multiple, multilevel, hierarchical, hybrid) and managing ambiguity (using
virtual inheritance) is crucial for efficient OOP programming.
Tasks 1:
Lab task 1: Write a program that has a class named BankAcnt that
have the properties of a simple bank account. Then inherit its
properties to SavingAcnt and CurrentAcnt Classes with their extra
features. Ask user for all account details.
Code:
Output:
Lab task 2: A car show room needs a software for listing their
vehicles, vehicles type and spare parts. There has to be all features
included that a vehicle have. Solve this problem using inheritance.
Code:
Output:
Observations:
In this lab we have learned about the parent of child class and we learned 5
parts of inheritance and how can we use it in our program and how can we
link them with each other and after that we learned how to access their
private or public members and we learned the there are three types of class
public, protected, private and after that we done some tasks.
Rubrics
Report not Content that The The Appropri Conclusion
submitted has been requirement observati ate drawn
plagiarized s are ons, as measure correctly
or that has described,
well as ments or with exact
Laborato been as well as
submitted the the numeric results and
ry
incompletel experiment complete al a complete
Reports
y al process. technique analysis report in
, are are every way
document carried
ed. out.
Category Ungrade Very Poor Poor Fair Good Excellent
d
Marks 0 1 2 3 4 5
Demonst Absent The The student The The student The student
ration student understands student has created successfully
is unable the followed a functional developed a
to presented instructio or working functional
adequat laboratory ns to build schematic, model,
ely instructions the core model, logic, circuit,
follow and is schematic block block
the familiar with , block diagram, or diagram, or
instructi the lab diagram, code and code and
ons environment code, or has completed
provided but Cannot model. successfull the lab
. The setup With y run the objective in
student simulation some program or real-time or
can according to assistance circuit on a in a
name design but , the software simulation
the knows how student platform. environment
hardwar to perform can set up With , achieving
e or simulation and run minimum the expected
simulatio the assistance, results. They
n simulation they can can set up,
platform, . On the set up and operate, and
but protoboar run the run the
Cannot d/trainer/s simulation. simulation
setup or imulation on their own.
perform software
the
simulatio
n
Category Ungrad Very Poor Fair Good Excellent
ed Poor
Marks 0 1 2 3 4 5