2nd year Computer Science Notes Chapter 8 Objects and Classes khyber pakhtunkhwa textbook board Peshawar Computer science book notes pdf download now.
Computer Science Notes Chapter 8 Objects and Classes
Q.4) Write a C++ program implementing a class with the name Circle having two functions with the names: GatRadius and CalArea. The functions should get the value for the radius from the user and then calculate the area of the circle and display the result.
Answer:
class Circle
{
public:
int GetRadius()
{
int radius;
cout << “Enter Radius of the circle :”;
cin >> radius;
return radius;
}
double CalArea(double p, double radius)
{
double area = p * radius * radius;
return area;
}
};
int main()
{
Circle c;
int r = c.GetRadius();
double p = 3.14;
cout << “Area of Circle is : ” << c.CalArea(p, r) << endl;
return 0;
}
Output:
The output of the above program is as follows:

Q.5) Write a C++ program implementing inheritance between Employee (base class) and Manager (derived class).
Answer:
class Employee
{
public:
void getBasicInfo()
{
cout << “– BASIC INFORMATION –\n”;
cout << “Name : Adeel Raheem.”;
cout << endl;
cout << “Emp. Id : 2206”;
cout << endl;
cout << “Gender : M”;
cout << endl;
}
void getDepartment()
{
string department;
cout << “– DEPT. INFORMATION –\n”;
cout << “Department Name : HR\n”;
cout << “Designation : Manager\n”;
cout << “Working Hours : 16 hrs/day\n\n”;
}
};
class Manager : public Employee
{
public:
void display()
{
getBasicInfo();
cout << “\n\n\n\n”;
getDepartment();
}
};
int main()
{
char c;
Manager m;
cout << “Do you want to get Employee Information? Select Y/N :”;
cin >> c;
cout << “\n\n”;
if (c == ‘Y’)
{
m.display();
}
else if (c == ‘N’)
{
cout << “\nNothing To Display”;
}
else
{
cout << “\nInvalid Selection”;
}
}
Output:
The output of the program is as follows:

Q.6) Diagrammatically represent the following scenario of inheritance:
- We have Shape as a base class and Circle, Rectangle, Triangle and square as its derived classes.

Q.7) Write a C++ program implementing a class with the name ConstDest having constructor and destructor functions in its body.
Answer:
class ConstDest
{
public:
ConstDest()
{
cout << “I am a Constructor” << endl;
}
~ConstDest()
{
cout << “I am a Destructor” << endl;
}
};
int main()
{
ConstDest c1;
_getch();
return 0;
}
Output:
The output of the program is as follows:

Q.8) Write a C++ program implementing a class with the name Time. This class should have a constructor to initialize the time, hours, minutes and seconds. The class should have another function named ToSecond() to convert and display the time into seconds.
Answer:
class Time
{
private:
int seconds;
int hh, mm, ss;
public:
void getTime(void);
void convertIntoSeconds(void);
void displayTime(void);
};
void Time::getTime(void)
{
cout << “Enter time:” << endl;
cout << “Hours? “; cin >> hh;
cout << “Minutes? “; cin >> mm;
cout << “Seconds? “; cin >> ss;
}
void Time::convertIntoSeconds(void)
{
seconds = hh * 3600 + mm * 60 + ss;
}
void Time::displayTime(void)
{
cout << “Time in total seconds: ” << seconds<<“\n\n”;
}
int main()
{
Time T;
T.getTime();
T.convertIntoSeconds();
T.displayTime();
return 0;
}
Output:
The output of the program is as follows:

Q.9) What is object? How objects are created to access members of a class?
Answer:
OBJECT
DEFINITION:
“Objects are instances of the class, which holds the data variables declared in the class and the member functions work on these class objects. In other words, a variable of type class is called an object.”
EXPLANATION:
In C++, when we declare a variable of type class, we call it instantiating (from the instance,) the class and the variable itself is called an instance or object of that class. The object is of great importance in OOP, because, a class cannot be used without creating its object.
GENERAL FORM:
The general syntax for creating an object of a class is given below.
Class_name Object_name;
Thus, for the above class CRectangle, the object can be created as follows:
CRectangle R1; II R1 is the object of class CRectangle
We can also create more than one object in a single statement like:
CRectangle R1, R2, R3;
EXPLANATION OF GENERAL FORM:
Here, R1, R2, R3 are the objects of the same class CRectangle that share the same data member and member functions. The definition of a class does not occupy any memory. It only defines what the class looks like. In order to use a class, a variable of that class type must be declared. When an object is created then memory is set aside for all the data members and member functions of that class.
EXAMPLE:
Consider the following program to implement the class CRectangle discussed above.
#include “stdafx.h”
#include <iostream>
#include<conio.h>
using namespace std;
class CRectangle
{
private:
int X, Y;
public:
void set_values(int a, int b)
{
X = a;
Y = b;
}
int area()
{
cout << “Area of the rectangle = ” << (X*Y);
return 0;
}
}; //End of class
int main()
{
CRectangle R1; // Object creation
R1.set_values(44, 22); //call to set_values function
R1.area(); // call to area function
_getch();
return 0;
}
EXPLANATION OF PROGRAM:
In this program the member function set-values and area are accessed by R1 .set-values(44,22) and R.area().
OUTPUT:
The output of the above example is as follows:

Q.10) Explain inheritance and polymorphism with examples.
Answer:
Inheritance
Definition:
“Acquiring some of the common qualities from parents (for instance eyes like mother, hair-like father, etc) is called inheritance.”
A new model car having some inherited features from the old model, like the brake system and navigation system, etc.
Example:
class Employee
{
public:
void getBasicInfo()
{
cout << “– BASIC INFORMATION –\n”;
cout << “Name : Adeel Raheem.”;
cout << endl;
cout << “Emp. Id : 2206”;
cout << endl;
cout << “Gender : M”;
cout << endl;
}
void getDepartment()
{
string department;
cout << “– DEPT. INFORMATION –\n”;
cout << “Department Name : HR\n”;
cout << “Designation : Manager\n”;
cout << “Working Hours : 16 hrs/day\n\n”;
}
};
class Manager : public Employee
{
public:
void display()
{
getBasicInfo();
cout << “\n\n\n\n”;
getDepartment();
}
};
int main()
{
char c;
Manager m;
cout << “Do you want to get Employee Information? Select Y/N :”;
cin >> c;
cout << “\n\n”;
if (c == ‘Y’)
{
m.display();
}
else if (c == ‘N’)
{
cout << “\nNothing To Display”;
}
else
{
cout << “\nInvalid Selection”;
}
}
Output:
The output of the program is as follows:

Polymorphism
Definition:
“It is the ability to use an operator or function in multiple ways.”
It means multiple possible states for a single property. For example, a person can be a student as well as a friend to another person. Also, a person can be an engineer as well as a teacher.
In C++ polymorphism can be achieved by one of the following concepts:
– Function overloading
– Operator overloading
– Virtual functions
Example:
Consider the following example:
6 + 10;
The above statement refers to integer addition with the help of ‘+’ operator. The same operator can be used for adding the floating point numbers. Or it can be used for concatenation of strings. They are as follows:
7.15 + 3.78
“Computer” + “Training”
The above concept is known as operating overloading.
Reated Post:
- 2nd-year computer science notes Chapter 1 Operating Systems
- 2nd-year computer science notes Chapter 2 System Development Life Cycle
- Computer Science Notes Chapter 5 Arrays and Strings
- Chapter 3 Object-Oriented Programming using C++
- 2nd year Computer science Notes Chapter 4 Control Structures
- Computer Science Notes Chapter 7 Pointers