Oct 2023 B.B.A (C.a) 2019 Pattern CPP - Solutions
Oct 2023 B.B.A (C.a) 2019 Pattern CPP - Solutions
(CA)
CA-402: OBJECT ORIENTED CONCEPTS THROUGH CPP
(2019 Pattern) (Semester - IV)
Paper Solution
Types of Inheritance:
1. Single Inheritance:
A derived class with only one base class is called as Single Inheritance. In this inheritance, a single class
inherits the properties of a base class. All the data members of the base class are accessed by the derived class
according to the visibility mode (i.e., private, protected, and public) that is specified during the inheritance.
It specifies access specifiers separately for all the base classes at the time of inheritance. The derived class
can derive the joint features of all these classes and the data members of all the base classes are accessed by
the derived or child class according to the access specifiers.
Syntax:
class base_class_1
{
// class definition
};
class base_class_2
{
// class definition
};
class derived_class: visibility_mode_1 base_class_1, visibility_mode_2
base_class_2
{
// class definition
};
The derived_class inherits the characteristics of two base classes, base_class_1 and base_class_2. The
visibility_mode is specified for each base class while declaring a derived class. These modes can be different
for every base class.
3. Multilevel Inheritance:
The mechanism of deriving a class from another derived class is called as Multilevel inheritance. The
inheritance in which a class can be derived from another derived class is known as Multilevel Inheritance.
Suppose there are three classes A, B, and C. A is the base class that derives from class B. So, B is the derived
class of A. Now, C is the class that is derived from class B. This makes class B, the base class for class C but
is the derived class of class A. This scenario is known as the Multilevel Inheritance. The data members of
each respective base class are accessed by their respective derived classes according to the specified visibility
modes.
Syntax
class class_A
{
// class definition
};
class class_B: visibility_mode class_A
Syntax
class class_A
{
// class definition
};
class class_B
{
// class definition
};
class class_C: visibility_mode class_A, visibility_mode class_B
{
// class definition
};
class class_D: visibility_mode class_C
{
int Product::cnt;
void main()
{
clrscr();
Product ob[10];
int n;
cout<<"\n Enter how many items : ";
cin>>n;
for(int i=0;i<n;i++)
ob[i].getdata();
for( i=0;i<n;i++)
ob[i].display();
getch();
}
b) Design a base class person (name, address, phoneno). Derive a class employee (eno,ename) from
person derive a class manager (designation, department, basic-salary) from Employee. Accept all
details of 'n' managers and display manager having highest basic salary.
Answer:
#include<iostream>
using namespace std;
class Person //Base Class
{
c) Write a C++ program to overload the functions to calculate area of circle, square and rectangle.
Answer:
#include<iostream>
using namespace std;
int area(int x)
{
return x*x;
}
int area(int l, int b)
{
return l*b;
}
double area(double r)
{
return 3.142*r*r;
}
int main()
{
int x, l, b;
double r;
cout<<"Enter the length of a square: ";
cin>>x;
cout<<"Enter the length of rectangle: ";
cin>>l;
cout<<"Enter the width of rectangle: ";
cin>>b;
cout<<"Enter the radius of circle: ";
cin>>r;
cout<<endl<<"The area of square is "<<area(x)<<endl;
cout<<endl<<"The area of rectangle is "<<area(l, b)<<endl;
cout<<endl<<"The area of circle is "<<area(r)<<endl;
}
e) Write a C++ program to accept length and width of a rectangle. Calculate and display perimeter of
a rectangle by using inline function.
Answer:
#include<iostream.h>
#include<conio.h>
class rectangle
{
float l,w;
public:
void getdata()
{
cout<<"\nEnter length of rectangle : ";
cin>>l;
cout<<"\nEnter width of rectangle : ";
cin>>w;
}
inline void Peri()
{
cout<<"\nPerimeter of rectangle = "<<2*(l+w);
}
};
void main()
MR. RAHUL SONAWANE 15
OCT 2023
{
clrscr();
rectangle obj;
obj.getdata();
obj.Peri();
getch();
}
constructor called
constructor called
Total object created:2
Given program uses cnt as a static member of class Number. Which is by default initialized to 0 (zero), and
single of cnt is shared among the objects of the class. As N1 and N2, two objects are created in driver program,
the parameterized constructor of class Number is invoked automatically and “constructor called” is
displayed twice, also cnt is incremented twice for both the objects.
Here, both Intermediate classes inherited from Base_class, and Derived_class inherits from both
Intermediate_class1 and Intermediate_class1.
Example:
#include <iostream>
using namespace std;
class Animal {
public:
Animal(string name) : name(name) {}
void eat() {
cout << name << " is eating." << endl;
}
private:
string name;
};
class Mammal : virtual public Animal {
public:
Mammal(string name) : Animal(name) {}
void run() {
cout << name << " is running." << endl;
}
};
class Bird : virtual public Animal {
public:
Bird(string name) : Animal(name) {}
void fly() {
cout << name << " is flying." << endl;
}
};
class Bat : public Mammal, public Bird {
public:
Bat(string name) : Animal(name), Mammal(name), Bird(name) {}
};
int main() {
Bat bat("XYZ");
MR. RAHUL SONAWANE 18
OCT 2023
bat.eat(); // Accessing Animal's method
bat.run(); // Accessing Mammal's method
bat.fly(); // Accessing Bird's method
return 0;
}
d) Write a C++ program to find maximum of two integer numbers using function template.
Answer:
#include<conio.h>
#include<iostream.h>
template <typename x> //template
using namespace std;
x big (x a,x b)
{
if(a>b)
{
return(a);
}
else
return(b);
}
int main()
{
int a,b,x,y;
cout<<"\nEnter 1st integer number : ";
cin>>a;
cout<<"\nEnter 2nd integer number : ";
cin>>b;
cout<<"\nEnter 1st float number : ";
cin>>x;
cout<<"\nEnter 2nd float number : ";
cin>>y;
cout<<"Maximum integer number is : "<<big<int>(a,b)<<endl;
cout<<"Maximum float number is : "<<big<float>(x,y);
getch();
return(0);
}
class String
{
public:
char str[20];
public:
void accept_string()
{
cout<<"\n Enter String :";
cin>>str;
}
void display_string()
{
cout<<str;
}
String operator+(String x) //Concatenating String
{
String s;
strcat(str,x.str);
strcpy(s.str,str);
return s;
}
};
int main()
{
String str1, str2, str3;
str1.accept_string();
str2.accept_string();
cout<<"\n\n First String is: ";
str1.display_string(); //Displaying First String
b) Access specifier
Answer:
Access specifiers are used to implement an important feature of Object-Oriented Programming known as
Data hiding. Access specifiers in a class define how the data members and functions of a class can be accessed.
That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
This access restriction to the class members is specified by the labeled public, private, and protected sections
within the class body. The keywords public, private, and protected are called access specifiers.
• public - members are accessible from outside the class but within a program.
• private - members cannot be accessed or viewed from outside the class. Only the class and friend functions
can access private members.
• protected - members cannot be accessed from outside the class, however, they can be accessed in inherited
classes.
public:
A (int k) //parameterized constructor of parent class.
{
a = k;
}
};
class B: public A
{
int b;
public:
B(int x, int y):A(x)
//constructor of child class calling constructor of base class.
{
b = y;
}
MR. RAHUL SONAWANE 22
OCT 2023
};
int main()
{
B obj(2,3);
}