0% found this document useful (0 votes)
14 views23 pages

Oct 2023 B.B.A (C.a) 2019 Pattern CPP - Solutions

The document is a paper solution for the B.B.A. (CA) course on Object-Oriented Concepts through C++. It covers key topics such as features of OOP, pure virtual functions, inheritance types, function overloading, static members, and friend functions, providing definitions and examples for each concept. The content is structured as a question and answer format, addressing various aspects of C++ programming relevant to the course curriculum.

Uploaded by

Ashish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views23 pages

Oct 2023 B.B.A (C.a) 2019 Pattern CPP - Solutions

The document is a paper solution for the B.B.A. (CA) course on Object-Oriented Concepts through C++. It covers key topics such as features of OOP, pure virtual functions, inheritance types, function overloading, static members, and friend functions, providing definitions and examples for each concept. The content is structured as a question and answer format, addressing various aspects of C++ programming relevant to the course curriculum.

Uploaded by

Ashish Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

B.B.A.

(CA)
CA-402: OBJECT ORIENTED CONCEPTS THROUGH CPP
(2019 Pattern) (Semester - IV)
Paper Solution

By- Rahul Sonawane

MR. RAHUL SONAWANE 1


OCT 2023
Q1) Attempt any eight of the following (out of ten) [8 × 2 = 16]
a) List any four features of OOP's.
Answer: The main features of object-oriented programming are as follows:
 Classes
 Objects
 Abstraction
 Polymorphism
 Inheritance
 Encapsulation
 Message Passing
 Dynamic binding
b) Define pure virtual function.
Answer: A "pure virtual function" in C++ is a special type of virtual function declared in a base class that has
no implementation (body) within the base class, meaning it must be overridden and defined by any derived
class; it is indicated by assigning " = 0" to the function declaration, effectively making the base class an
"abstract base class" which cannot be directly instantiated to create objects.
c) What is cascading of I/O operator?
Answer: The cascading of I/O operators, is nothing but the consecutive occurrence of input or output
operators in a single statement.
In C++, the cascading I/O operator refers to the use of the stream insertion << and extraction >> operators in
a way that allows multiple input or output operations to be chained together in a single statement. This makes
the code more concise and readable.
Example of Cascading Input and Output
#include <iostream>
int main()
{
int x, y;
std::cout << "Enter two integers: ";
std::cin >> x >> y;
std::cout << "You entered: " << x << " and " << y << std::endl;
return 0;
}
d) List the ways to define a constant.
Answer: Types of ways of defining Constants in C++
 Using const Keyword
 Using constexpr Keyword
 Using #define Preprocessor

e) What is an abstract class?

MR. RAHUL SONAWANE 2


OCT 2023
Answer: In C++, an abstract class is a class that cannot be instantiated on its own, but can be used as a base
class for other classes. Abstract classes are used to express general concepts and provide a blueprint for derived
classes. An abstract class contains at least one pure virtual function. We declare a pure virtual function by
using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.
The following is an example of an abstract class:
class AB {
public:
virtual void f () = 0;
};
f) Define multiple inheritance.
Answer: In C++ programming, a class can be derived from more than one parent. The constructors of
inherited classes are called in the same order in which they are inherited. For example, A class Bat is derived
from base classes Mammal and WingedAnimal. It makes sense because bat is a mammal as well as a winged
animal.
Syntax:
class Mammal
{
... .. ...
};
class WingedAnimal
{
... .. ...
};
class C: public Mammal, public WingedAnimal.
{
... ... ...
};
g) Define destructor.
Answer: A destructor is a member function that is invoked automatically when the object goes out of scope
or is explicitly destroyed by a call to delete or delete[]. It deallocates memory and performs other cleanup
tasks for the object.
A destructor has the same name as the class and is preceded by a tilde ( ~ ).
For example, the destructor for class String is declared: ~String().
h) What is 'this' pointer?
Answer: The 'this' pointer in C++ is a special, implicit pointer available within all non-static member
functions of a class. It helps a member function access the current object instance on which it is being called.
i) What is Run-Time Polymorphism?
Answer: In runtime polymorphism, the compiler resolves the object at run time and then it decides which
function call should be associated with that object. It is also known as dynamic or late binding polymorphism.
This type of polymorphism is executed through virtual functions and function overriding.

MR. RAHUL SONAWANE 3


OCT 2023
j) Enlist manipulators in C++.
Answer:
C++ Manipulators
 endl : It is used to enter a new line with a flush.
 setw(a): It is used to specify the width of the output.
 setprecision(a): It is used to set the precision of floating-point values.
 setbase(a): It is used to set the base value of a numerical number.
 setfill(char c): Fills the remaining space with the specified character.

Q2) Attempt any FOUR of the following (out of FIVE) [4 × 4 = 16]


a) Explain function overloading with example.
Answer: Function overloading is a feature of object-oriented programming where two or more functions can
have the same name but different parameters. When a function name is overloaded with different jobs it is
called Function Overloading. In Function Overloading “Function” name should be the same and the
arguments should be different. It is one of the salient features of C++. Function overloading can also be treated
as compile-time polymorphism.
For example:
// same name different arguments
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
Example: Overloading Using Different Number of Parameters
#include <iostream>
using namespace std;
void display(int var1, double var2)
{
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}
void display(double var)
{
cout << "Double number: " << var << endl;
}
void display(int var)
{
cout << "Integer number: " << var << endl;
}
int main()
{
int a = 5;
double b = 5.5;

MR. RAHUL SONAWANE 4


OCT 2023
display(a);
display(b);
display(a, b);
return 0;
}
Here, the display() function is called three times with different arguments. Depending on the number and type
of arguments passed, the corresponding display() function is called.
b) What is inheritance? Explain types of inheritance.
Answer: The mechanism of deriving a new class from an old class is called as Inheritance. Inheritance allows
a derived class to inherit the properties and characteristics from base class.
A class can also inherit properties from more than one class or from more than one level. Inheritance supports
the reusability as inheritance can extend the use of existing classes and eliminate redundant code.
The class that inherits the properties from another class is called Sub class or Derived Class. The class whose
properties are inherited by derived class is called Super class or BaseClass.
Syntax to define derived class:
class Derived_class_name : visibility_mode Base_class_name
{
//body of Derived class
};
Where,
 Derived_class_name is the name of the sub class/derived class.
 visibility_mode specifies the mode in which derived class can be inherited. For example: public,
private, protected. Default visibility mode is private.
 Base_class_name is the name of the base class from which you want to inherit the sub class.

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.

Syntax to define derived class:


class Derived_class: visibility_mode Base_class
{
//Body of Derived class

MR. RAHUL SONAWANE 5


OCT 2023
};
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit from more than
one classes. i.e one sub class is inherited from more than one base class.

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.

MR. RAHUL SONAWANE 6


OCT 2023
Syntax:
class class_A
{
// class definition
};
class class_B: visibility_mode class_A
{
// class definition
};
class class_C: visibility_mode class_B
{
// class definition
};
The class_A is inherited by the sub-class class_B. The class_B is inherited by the subclass class_C. A
subclass inherits a single class in each succeeding level.
4. Hierarchical Inheritance:
More than one derived classes inherit the features from a single base class is called as Hierarchical
Inheritance i.e. more than one derived classes are created from a single base class.
The inheritance in which a single base class inherits multiple derived classes is known as the Hierarchical
Inheritance. This inheritance has a tree-like structure since every class acts as a base class for one or more
child classes. The visibility mode for each derived class is specified separately during the inheritance and it
accesses the data members accordingly.

Syntax
class class_A
{
// class definition
};
class class_B: visibility_mode class_A

MR. RAHUL SONAWANE 7


OCT 2023
{
// class definition
};
class class_C : visibility_mode class_A
{
// class definition
};
class class_D: visibility_mode class_B
{
// class definition
};
class class_E: visibility_mode class_C
{
// class definition
};
The subclasses class_B and class_C inherit the attributes of the base class class_A. Further, these two
subclasses are inherited by other subclasses class_D and class_E respectively.
5. Hybrid Inheritance:
Hybrid Inheritance, as the name suggests, is the combination of two or over two types of inheritances. For
example, the classes in a program are in such an arrangement that they show both single inheritance and
hierarchical inheritance at the same time. Such an arrangement is known as the Hybrid Inheritance. This is
arguably the most complex inheritance among all the types of inheritance in C++. The data members of the
base class will be accessed according to the specified visibility mode.

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
{

MR. RAHUL SONAWANE 8


OCT 2023
// class definition
};
class class_E: visibility_mode class_C
{
// class definition
};
The derived class class_C inherits two base classes that are, class_A and class_B. This is the structure of
Multiple Inheritance. And two subclasses class_D and class_E, further inherit class_C. This is the structure of
Hierarchical Inheritance. The overall structure of Hybrid Inheritance includes more than one type of
inheritance.
c) Explain static data members and static member functions with example.
Answer:
Static data members:
 Static data members are class members that are declared using the static keyword.
 The normal variable is created when the function is called and its scope is limited, while the static
variable is created once and destroyed at the end of the program.
 These variables are visible within the class but its lifetime is till the program ends.
 There is only one copy of the static data member in the class, even if there are many class objects.
 This is because all the objects share the static data member. To hold the count of objects created for a
class, static data members are used.
 The static data member is always initialized to zero when the first class object is created.
 While defining a static variable, some initial value can also be initialized to the variable.
 Type and scope of each static member variable must be defined outside the class definition using scope
resolution operator. This is necessary because the static data members are stored separately rather than
as a part of an object.
 Static data members are associated with the class itself rather than with any class object, hence they
are also known as class variables.
Static member functions:
 Like static data member, we can also have static member functions. A static member function can only
access other static variables or functions present in the same class.
 To create a static member function we need to use the static keyword while declaring the function.
 Since static member variables are class properties and not object properties, to access them we need
to use the class name instead of the object name.
 A static member function can be called even if no objects of the class exist and the static functions are
accessed using class name and the scope resolution operator::.
 You could use a static member function to determine whether some objects of the class have been
created or not.
Example:
C++ program to illustrate use of static data member and static member function.
#include <iostream.h>
class StaticDemo

MR. RAHUL SONAWANE 9


OCT 2023
{
private:
static int num; //declaration of static data member
public:
static void Display() //static member function definition
{
cout<<"Value of num is : "<<num<<endl;
}
};
int StaticDemo::num=10; //static data member definition and initialization
out side class
int main()
{
Static Demo::Display(); //call to static member function
return0;
}
Output:
Value of num is: 10
d) What is friend function? Write characteristics of friend function.
Answer:
Friend Function:
A friend function of a class is defined outside that class scope but it has the right to access all private and
protected members of the class.
For accessing the data, the declaration of a friend function should be done inside the body of a class starting
with the keyword friend. A friend function of a class is defined outside that class' scope but it has the right to
access all private and protected members of the class.
Even though the prototypes for friend functions appear in the class definition, friends are not member
functions. The function can be defined anywhere in the program like a normal C++ function. The function
definition does not use either the keyword friend or scope resolution operator.
Declaring Friend Function
To declare a function as a friend of a class, precede the function prototype in the class definition with the
keyword friend as follows −
Syntax:

// Creating a class named class_Name


class class_Name
{
// declartion of class properties

friend return_type function_Name(Argument_1,...,Argument_5);


};

Characteristics of a Friend Function:


MR. RAHUL SONAWANE 10
OCT 2023
 A global function or a member function of another class, both can be declared as a friend function.
 Friend function is not in the scope of the class to which it has been declared as a friend.
 It cannot be called using the object as it is not in the scope of that class.
 It can be invoked like a normal function without using the object.
 It cannot access the member names directly and has to use an object name and dot membership operator
with the member name.
 It can be declared either in the private or the public part.
 Friend functionality in C++ is not restricted to only one class. That is, it can be a friend to many classes.
 Friend functions in C++ can use objects (instance of a class) as arguments.

e) Explain use of any four file opening modes.


Answer:
File is collection of data or information. To perform input and output operations on files, ifstream , ofstream
and fstream classes included in the <fstream.h> library. File can be opened by using member function open()
or by using constructor. In C++, for every file operation, exists a specific file mode. These file modes allow
us to create, read, write, append or modify a file. The file modes are defined in the class ios.There are different
modes (flags) to open a file.
Parameter Meaning
ios::in Open for input operations.
Searches for the file and opens it in the read mode only(if the file is found).
ios::out Open for output operations.
Searches for the file and opens it in the write mode.
If the file is found, its content is overwritten. If the file is not found, a new file is created.
Allows us to write to the file.
ios::binary Open in binary mode. Searches for the file and opens the file(if the file is found) in a binary
mode to perform binary input/output file operations.
ios::ate Searches for the file, opens it and positions the pointer at the end of the file.
This mode when used with ios::binary, ios::in and ios::out modes, allows us modify the
content of a file.
Set the initial position at the end of the file. If this flag is not set, the initial position is the
beginning of the file.
ios::app Searches for the file and opens it in the append mode i.e. this mode allows you to append
new data to the end of a file.
If the file is not found, a new file is created.
All output operations are performed at the end of the file, appending the content to the
current content of the file.
ios::trunk If the file is opened for output operations and it already exists, its previous content is deleted
and replaced by the new one.

Q3) Attempt any Four of the following (out of FIVE) [4 × 4 = 16]


a) Write a C++ program to create a class product which contains data members as pname, price,
quantity. Write member functions to accept quantity for n products and accordingly generate and
display bill.

MR. RAHUL SONAWANE 11


OCT 2023
Answer:
#include<iostream.h>
#include<conio.h>
class Product
{
int price,Qty;
char pname[20];
static int cnt;
public:
void getdata()
{
cout<<"\n Enter Product name\t\t";
cin>>pname;
cout<<"\n Enter Product price\t";
cin>>price;
cout<<"\n Enter Product Qty\t";
cin>>Qty;
cnt++;
}
void display()
{
cout<<"\nProduct name =\t"<<pname;
cout<<"\nProduct price =\t"<<price;
cout<<"\nProduct QTY =\t"<<Qty;
}
};

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
{

MR. RAHUL SONAWANE 12


OCT 2023
protected:
char pname[50], address[100];
int phone_no;
};
//Class Employee - Derived Class from Person.
class Employee : public Person
{
public:
int eno;
char ename[50];
};
//Class Manager - Derived Class from Class Employee
class Manager : public Employee
{
public:
char designation[50], deptname[100];
float basic_salary;
public:
void accept_details()
{
cout<<"\n Enter Details of Manager ";
cout<<"\n -------------------------- ";
cout<<"\n Enter Employee No. : ";
cin>>eno;
cout<<"\n Enter Name : ";
cin>>ename;
cout<<"\n Enter Address : ";
cin>>address;
cout<<"\n Enter Phone No. : ";
cin>>phone_no;
cout<<"\n Enter Designation : ";
cin>>designation;
cout<<"\n Enter Department Name : ";
cin>>deptname;
cout<<"\n Enter Basic Salary : ";
cin>>basic_salary;
}
};
int main()
{
int i,cnt,temp;
Manager man[100];
cout<<"\n How Many Managers You Want to Enter? : ";
cin>>cnt;
for(i=1;i<=cnt;i++)
{
man[i].accept_details();
}
temp=0;
for(i=1;i<=cnt;i++)
{
if(man[temp].basic_salary<man[i].basic_salary)
temp=i;

MR. RAHUL SONAWANE 13


OCT 2023
}
cout<<"\n Manager with Highest Salary is :
"<<man[temp].basic_salary;
cout<<" \n And, Manager Name is : "<<man[temp].ename;
return 0;
}

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

d) Write a C++ program to print the following pattern


A
BC

MR. RAHUL SONAWANE 14


OCT 2023
DEF
GHIJ
Answer:
#include<conio.h>
#include<iostream.h>
void main()
{
int i,j,n;
char c;
clrscr();
cout<<"Eneter the no of lines to be printed: ";
cin>>n;
c='A';
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
if(c=='Z')
break;
cout<<c;
c++;
}
cout<<endl;
}
getch();
}

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

Q4) Attempt any Four of the following (out of Five) [4 × 4 = 16]


a) Trace output of following program and explain it. Assume there is no syntax error.
# include <iostream.h>
Class Number
{
Public :
int a, b ;
static int cnt ;
Number (int x, int y)
{
cout <<"\n constructor called";
a = x ;
b = y ;
cnt ++ ;
}
void display ( )
{
cout <<"\n a =" <<a<< "\n b =" <<b;
}
};
int Number::cnt ;
void main ( )
{
Number N1(4, 6), N2(2, 8);
cout <<"\n Total object created:"<<Number:: cnt ;
}
Answer:
Output:

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.

b) Explain parameterized constructor with the help of suitable example.


Answer:
MR. RAHUL SONAWANE 16
OCT 2023
A parameterized constructor in C++ is a type of constructor that takes arguments (parameters). Giving
arguments allows you to initialize an object with specific values when you create it instead of always
providing the object's default values. The constructors can be called explicitly or implicitly.
If more than one constructor is defined in a class, it is called as Constructor Overloading.
Example: To illustrate the use of Parameterized Constructor.
#include<iostream.h>
class Number
{
int n;
public:
Number(intx) //ParameterizedConstructor
{
n = x;
}
};
int main()
{
Number Obj1=Number(50); // Explicit call
NumberObj2(100); // Implicit call
}
In the above program, we have created a parameterized constructor to initialize the member variable a with
the value given by the user. It is passed to the constructor at the time of object definition. Number Obj1 &
Number Obj2 invokes parameterized constructor and initializes data member n to 50 & 100 respectively.

c) Explain virtual base class with example.


Answer:
 Virtual base classes in C++ are essential for resolving ambiguity and eliminating redundant copies of
common base class members in complex class hierarchies.
 In C++, when we have a class that is inherited by multiple derived classes, we might encounter a problem
known as the "diamond problem". This happens when a class inherits from two or more classes that have
a common base class.
 Several paths exist to a derived class from the same base class i.e.a derived class can have duplicate sets
of members inherited from a single base class. This introduces ambiguity and it should be avoided.
 Duplication of inherited members due to multiple paths is avoided by making the common base class as
virtual base class. This is achieved by preceding the base class name with the keyword virtual.

 Here, both Intermediate classes inherited from Base_class, and Derived_class inherits from both
Intermediate_class1 and Intermediate_class1.

MR. RAHUL SONAWANE 17


OCT 2023
 If you try to access a member of Base_class through Derived_class, the compiler might get confused
about which instance of Base_class to use.
Virtual base class in C++ syntax
class Base{
public:
// Base class members and methods
};
class Derived1 : virtual public Base{
public:
// Derived1 class members and methods
};
class Derived2 : virtual public Base{
public:
// Derived2 class members and methods
};
class MostDerived : public Derived1, public Derived2{
public:
// MostDerived class members and methods
};

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

 Both Mammal and Bird inherit virtually from Animal.


 Class Bat inherits from both Mammal and Bird.
 When we create a Bat object:
o We avoid the diamond problem by using virtual inheritance for Mammal and Bird.
o This ensures there's only one instance of the Animal base class.

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

e) Write a program to overload binary plus operator to concatenation of two strings.


Answer:
#include<iostream>

MR. RAHUL SONAWANE 19


OCT 2023
#include<string.h>
using namespace std;

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

cout<<"\n\n Second String is: ";


str2.display_string(); //Displaying Second String
str3=str1+str2; //String is concatenated. Overloaded '+' operator
cout<<"\n\n Concatenated String is:";
str3.display_string();
return 0;
}

Q5) Write a short note on any two of the following. [2 × 3 = 6]


a) Array of object
Answer:
An object of class represents a single record in memory, if we want more than one record of class type, we
have to create an array of object.
An array which contains the class type of element is called array of objects.
Array of objects contains the objects of the class as its individual elements. It is declared in the same way as
an array of any built-in data type.

MR. RAHUL SONAWANE 20


OCT 2023
Syntax:

ClassName ObjectName[number of objects];

C++ program to illustrate use of array of objects.


#include<iostream.h>
class Employee
{
int Emp_id;
char Name[20];
long Salary;
public:
void Accept()
{
cout<<"\n\tEnter Employee Id, Name and Salary : ";
cin>>Emp_id>>Name>>Salary;
}
void Display()
{
cout<<"\n"<<Emp_id<<"\t"<<Name<<"\t"<<Salary;
}
};
int main()
{
int i;
Employeeemp[3];
//Creating Array of objects to store3 Employees details
for(i=0;i<3;i++)
{
cout<<"\nEnter details of "<<i+1<<" Employee"; emp[i].Accept();
}
cout<<"\nDetails of Employees"; for(i=0;i<3;i++) emp[i].Display();
return 0;
}
Above program will accept and display details of 3 employees using array of objects.

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.

MR. RAHUL SONAWANE 21


OCT 2023
But if we do not specify any access specifier for the members inside the class then by default the access
specifier for the members will be private. Member functions of the class can access all the data members and
other member functions of the same class (private, public or protected) directly by using their names.
A public member can be accessed from outside the class anywhere within the scope of the class object hence
side is accessible in main function through object of square class. You can also specify the members of a class
as private or protected as per the need.

c) Constructor in derived class


Answer:
In C++, constructors in derived classes are special member functions used to initialize objects of derived
classes. When we create an object of a derived class, its constructor is called to initialize both the base class
part and the derived class part of the object. Constructors in derived classes can call constructors of their base
classes explicitly to ensure proper initialization of the inherited members.
While applying inheritance, we usually create objects using derived class, thus derived class must pass
arguments to the base class constructor. When both the derived and base class contains constructors, the base
constructor is executed first and then the constructor in the derived class is executed.
In case of multiple inheritance, the base class is constructed in the same order in which they appear in the
declaration of the derived class. Similarly, in a multilevel inheritance, the constructor will be executed in the
order of inheritance.
The derived class takes the responsibility of supplying the initial values to its base class. The constructor of
the derived class receives the entire list of required values as its argument and passes them on to the base
constructor in the order in which they are declared in the derived class. A base class constructor is called and
executed before executing the statements in the body of the derived class.
For Example:
class A
{
int a;

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



MR. RAHUL SONAWANE 23


OCT 2023

You might also like