Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CS112L - Object Oriented Programming Lab: Air University Islamabad

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CS112L – Object Oriented Programming Lab

Topic: Inheritance

Class: BSCYS
Semester: II
Session: Spring, 2021
Instructor: Dr. Naveed
Lab Instructor: Ms. Hina Batool

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL
INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

Instructions

Submission: Use proper naming convention for your submission file. Name the submission file
as LabNO_ROLLNUM (e.g. Lab01_00000). Submit the file on Google Classroom within the
deadline. Failure to submit according to the above format would result in deduction of 10%
marks. Submissions on the email will not be accepted.

Plagiarism: Plagiarism cases will be dealt with strictly. If found plagiarized, both the involved
parties will be awarded zero marks in the assignment, all of the remaining assignments, or even
an F grade in the course. Copying from the internet is the easiest way to get caught!

Deadline: The deadlines to submit the assignment are hard. Late submission with marks
deduction will be accepted according to the course policy shared by the instructor. Correct and
timely submission of the assignment is the responsibility of every student; hence no relaxation
will be given to anyone.

Comments: Comment your code properly. Bonus marks (maximum 10%) will be awarded to
well comment code. Write your name and roll number (as a block comment) at the beginning of
Objectives
the solution to each problem.
In this lab, you will learn:
Tipo: For timely
About completion
Structure ADT. of the assignment, start as early as possible. Furthermore, work
o How
smartly - as to
some of the
define problemsinitialize
a Structure, can be solved using
and refer smarter logic.
to individual members of a Structure.
Note: Follow the given instructions to the letter, failing to do so will result in a zero.

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

Objectives
In this lab, you will learn about type Conversion and following:
 Inheritance in Classes
 Multiple Inheritance
 Multilevel Inheritance
 Hybrid Inheritance
 Accessing Base Class Members
 Sequence of Constructor and Destructor

Concepts
1. Inheritance in Classes
Inheritance is one of the core features of an object-oriented programming language. It allows software
developers to derive a new class from the existing class. The derived class inherits the features of the
base class (existing class). There are various types of inheritance in OOP C++ programming which
will be discussed in later Sections. The simple example of Inheritance:

Output:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

2. Multi-Level Inheritance:
In C++ programming, not only you can derive a class from the base class but you can also derive a
class from the derived class. This form of inheritance is known as multilevel inheritance.

class A {
... .. ... };

class B: public A {
... .. ... };

class C: public B {
... ... ... };

The following is an example of Multi-level Inheritance:

Output:

3. Multiple Inheritance:
Multiple Inheritance is a feature of OOP where a class can inherit from more than one classes i.e.
one sub class is inherited from more than one base classes. The following is syntax to demonstrate
the concept behind:

class subclass_name : access_mode base_class1, access_mode base_class2, ....


{
//body of subclass Air University Islamabad
}; FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

The following is an example of Multiple Inheritance:

Output:

4. Hybrid Inheritance:
In this type of inheritance, more than one sub class is inherited from a single base class. i.e.
more than one derived class is created from a single base class. The following is an example:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

Output:

5. Accessing Base Class Members:


Public members of base class become public member of derived class. Private members of
base class are not accessible from outside of base class, even in the derived class (Information
Hiding)
Air University Islamabad
FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

Example:
In the code given below Student and Teacher classes has been derived from single Person class.

class Person {
string name;
int age;
/*...*/
public:
string GetName();
int GetAge();
/*...*/
};

class Teacher : public class Student : public


Person { Person {
string dept; int semester;
int course; int rollNo;
/*...*/ /*...*/
public: public:
string GetDept(); int GetSemester();
int GetCourse(); int GetRollNo();
void Print(); void Print();
/*...*/ /*...*/
}; };

void Student::Print()
{
cout << name << " is in" << " semester " << semester;
}
//corrected Code :
void Student::Print()
{
cout << GetName() << " is in semester " << semester;
}

int main() {
Student stdt;
stdt.semester = 0;//error
stdt.name = “sara”; //error
cout << stdt.GetSemester();
cout << stdt.GetName();
return 0;
}
Air University Islamabad
FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

6. Sequence of Constructor/Destructor:
The anonymous object of base class must be initialized using constructor of base class. When a
derived class object is created the constructor of base class is executed before the constructor
of derived class.

Example:

class Parent {
public:
Parent() {
cout <<
"Parent Constructor...";
}
};
class Child : public Parent {
public:
Child() {
cout <<
"Child Constructor...";
}
};

int main() {
Child cobj;
return 0;
}

Output:

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security
CS112L– Object Oriented Programming Lab

Lab Tasks
1. (Simple Inheritance) Write a C++ program to design a base class Person (name, address,
phone_no). Derive a class Employee (eno, ename) from Person. Derive a class Manager
(designation, department name, and basic-salary) from Employee. Write a menu driven
program to:
o Accept all details of 'n' managers.
o Display manager having highest salary.

2. PakWheel required a system that manages their vehicles. Vehicles are of two types: Car and Truck. For
the Car extra feature is “Sitting Capacity” and for Truck it is Loading capacity in weight. Each Vehicle
contains Vehicle Body Feature (Color, Type and Material).. Use constructor for Initialization.

3. (Multi-level Inheritance) Write a C++ program to calculate the percentage of a student using
multi-level inheritance. Accept the marks of three subjects in base class. A class will be derived
from the above mentioned class which includes a function to find the total marks obtained and
another class derived from this class which calculates and displays the percentage of the
student.

Air University Islamabad


FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE
Department of Cyber Security

You might also like