You are on page 1of 21

BCS-031 C++ Programming

Constructor & Destructor

1. What is constructor?
Explain the advantages of constructor with the help of an example.
Write the characteristics and limitations of a constructor?
Explain how constructor is overloaded in C++, with the help of an example.

Ans. Constructor is a special member function of a class that initializes the object
of the class. Constructor name is same as class name and it doesn’t have a return
type.
When you create an object, if you do not declare a constructor the compiler
would create one for your program; this is useful because it lets all other objects
and functions of the program know that this object exists.

Eg: Constructor
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
public:
student()
{
cout<<"Object is initialized"<<endl;
}
};

int main()
{
student x,y,z;
getch();
}

Output:
Object is initialized
Object is initialized
Object is initialized
Characteristics:

1. They should be declared in public section


2. They are invoked directly when an object is created
3. They don’t have return type
4. They can’t be inherited
5. Constructors can’t be virtual
6. It can be either inside or outside the class definition
7. Constructor can’t be Friend Function
8. They can’t be used in Union

Limitation

1. No return type
2. Naming is same as Class, so we can’t have 2 constructors that both take single
arguments
3. Compile time bound
4. There is no virtual constructor

Constructor Overloading
When more than one constructor function is defined in a class, then it is
called constructor overloading
Below example student constructor has been defined in many ways and the
output of the program is student name and his roll no.
Eg: Constructor Overloading

#include<iostream>
#include<conio.h>
using namespace std;
Class student
{
int roll;
char name[30];
public:
student(int x,char y[]) //Parameterized constructor
{
roll=x;
strcpy(name,y);
}
student() //Normal constructor
{
roll=100;
strcpy(name,”Mollutty”);
}
void input_data()
{
cout<<”\n Enter Roll no:”;
cin>>roll;
cout<<”\n Name: “;
cin>>name;
}
void show_data()
{
cout<<”\nRoll no: ”<<roll;
cout<<”\n Name: “<<name;
}
};

int main()
{
student s(165574800,”Vipin”);
s.show_data();
getch();
}
2. Short note on Copy constructor. Explain copy constructor with the help of an
example program.

Ans. A copy constructor is used to declare and initialize an object from another object. A
copy constructor is always used when the compiler has to create a temporary
object of a class object. copy constructors are used in the following situations

 Initialization of an object by another object of the same class


 Return of objects as a function value
 Stating the object as by value parameters of a function

Eg1:

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

class student
{
int roll;
char name[30];

public:

student() //Default Constructor


{
roll=165574800;
strcpy(name,"Vipin");
}

student(student &o) //Copy Constructor


{
roll=o.roll;
strcpy(name,o.name);
}
void input_data()
{
cout<<"\nEnter Roll No: ";
cin>>roll;
cout<<"\nEnter Name: ";
cin>>name;
}

void show_data()
{
cout<<"\nRoll No: "<<roll;
cout<<"\nName: "<<name;
}
};

int main()
{
student s; //Default Constructor
s.show_data();
student A(s); //Copy Constructor
A.show_data();
getch();
}

3. How does Constructor differ from Automatic Initialization?

Ans. Constructor needs to initialize before it is used and it is not automatically


incremented or decremented during the execution of program, while automatic
initialization takes place automatically without any nitration of the user

4. What is a Destructor in C++? And its needs


Discuss the naming conventions of destructor. Do constructors and destructors
have return type?
Explain the role of destructors in C++ memory management.
Write a program in C++ to demonstrate the use of destructors.
Ans. Destructors are functions that are complimentary to constructors. A
destructor is used to destroy the objects that have been created by using the
constructor. A destructor is invoked when an object goes out of scope or when the
memory occupied by it is de-allocated using the delete operator. A destructor is a
function that has the same name as the class but is prefixed with ~ (Tilde).
Destructors have no return type. It is called automatically by the compiler when an
object is destroyed. A destructor cleans up the memory that is no longer required
or accessible. We can have only one destructor for a class can’t be overloaded.

#include<iostream>
#include<conio.h>
using namespace std;

class xyz
{
public:

xyz()
{
cout<<"\nConstructor Invoked";
}

~xyz()
{
cout<<"\nDestructor Invoked";
}
};

int main()
{
xyz s;
getch();
}

Output:

Constructor Invoked (Enter)


Destructor Invoked
5. Short note on Default and Parameterized Constructor

Ans. Default constructor is a constructor that either has no parameters or if it has


parameters all the parameters have default value. When you don’t specify any
constructor in the class, a default constructor with no code (empty body) would be
inserted into your code by compiler.

Parameterized Constructor is a constructor which can accept parameters for


its invocation, the constructor that can take arguments. Declaring a constructor
with arguments hides the Default Constructor.

6. Write a program in C++ to multiply two complex numbers. In this program you
need to create a complex class and define a proper constructor for object
initialization. Give suitable comments in your program.

7. Differentiate between copy constructor and default constructor in C++, with the
help of an example for each.

Ans. Program in Question 2 (Eg1:)

Function Template & Template Class

1. What is function template? Write a function template SUM to add two numbers.

2. What is template class? Explain the advantages of template class.

3. What is class template?


Give two advantages of class template.
Create a class template for stack data structure.
Create a class template for a linked list data structure.
4. Compare Class templates and Function templates with the help of example code

Inheritance

1. List the merits and demerits of single inheritance over multiple inheritance.

2. What is Inheritance?
Explain different types of inheritance in C++.
What are the advantages of inheritance? Explain with the help of example.
Explain how inheritance is implemented in C++

Ans. Inheritance is one of the feature of Object Oriented Programming


System(OOPs), it allows the child class to acquire the properties (the data
members) and functionality (the member functions) of parent class.

Another important point to note is that when we create the object of child
class it calls the constructor of child class and child class constructor automatically
calls the constructor of base class.

Advantages:
The main advantages of inheritance are code reusability and readability.
When child class inherits the properties and functionality of parent class, we need
not to write the same code again in child class. This makes it easier to reuse the
code, makes us write the less code and the code becomes much more readable.

Eg of Inheritance:

Here we have two classes Teacher and MathTeacher, the MathTeacher class
inherits the Teacher class which means Teacher is a parent class and MathTeacher
is a child class. The child class can use the property collegeName of parent class.
#include <iostream>
#include<conio.h>
using namespace std;

class Teacher
{
public:
Teacher()
{
cout<<"Hey Guys, I am a teacher"<<endl;
}
string collegeName = "IGNOU";
};

class MathTeacher: public Teacher //This class inherits Teacher class


{
public:
MathTeacher()
{
cout<<"I am a Math Teacher"<<endl;
}
string mainSub = "Math";
string name = "Vipin";
};

int main()
{
MathTeacher obj;
cout<<"Name: "<<obj.name<<endl;
cout<<"College Name: "<<obj.collegeName<<endl;
cout<<"Main Subject: "<<obj.mainSub<<endl;
getch();
}
Different Types of inheritance

1. Single Inheritance
Derivation of a class from only one base class is called a Single Inheritance.
For example: Let’s say we have class A and B, B inherits A.

#include <iostream>
#include<conio.h>
using namespace std;

class A
{
public:
A()
{
cout<<"Constructor of A class"<<endl;
}
};

class B: public A
{
public:
B()
{
cout<<"Constructor of B class";
}
};

int main()
{
B obj; //Creating object of class B
getch();
}
Output:
Constructor of A class
Constructor of B class

Here we can see that the child class B creates an object which would invoke
the constructor of class B but here we can see constructor of class A also invoked as this is
parent class to class B
2. Multiple inheritance
Derivation of a class from two or more base classes is called multiple
inheritance. This means that in this type of inheritance a single child class can have
multiple parent classes.

For Example C inherits A and B both

#include <iostream>
#include<conio.h>
using namespace std;

class A
{
public:
A()
{
cout<<"Constructor of A class"<<endl;
}
};

class B
{
public:
B()
{
cout<<"Constructor of B class"<<endl;
}
};

class C: public A, public B


{
public:
C()
{
cout<<"Constructor of C class"<<endl;
}
};
int main()
{
C obj; //Creating object of class C
getch();
}

Output:
Constructor of A class
Constructor of B class
Constructor of C class

Here we can see that the child class C creates an object which would invoke the
constructor of class C but here we can see constructor of class A and class B also
invoked as this is parent classes to class C

3. Multiple Level Inheritance


The class inherits the feature of another derived class is called multi-level
inheritance.

For Example C inherits B and B inherits A

#include <iostream>
#include<conio.h>
using namespace std;

class A
{
public:
A()
{
cout<<"Constructor of A class"<<endl;
}
};

class B: public A
{
public:
B()
{
cout<<"Constructor of B class"<<endl;
}
};

class C: public B
{
public:
C()
{
cout<<"Constructor of C class"<<endl;
}
};

int main()
{
C obj; //Creating object of class C
getch();
}

Output
Constructor of A class
Constructor of B class
Constructor of C class

4. Hierarchical Inheritance
Derivation of several classes from a single base class is called Hierarchical
Inheritance.

For Example: Class B and C inherits class A


#include <iostream>
#include<conio.h>
using namespace std;

class A
{
public:
A()
{
cout<<"Constructor of A class"<<endl;
}
};

class B: public A
{
public:
B()
{
cout<<"Constructor of B class"<<endl;
}
};

class C: public A
{
public:
C()
{
cout<<"Constructor of C class"<<endl;
}
};

int main()
{
C obj; //Creating object of class C
getch();
}

Output:
Constructor of A class
Constructor of C class
5. Hierarchical Inheritance
Derivation of a class involving more than one form of inheritance is called
Hybrid inheritance.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

3. Compare Multi-level and Multiple inheritance in C++ with the help of an example.

4. Explain the access specifiers used in inheritance in C++ with the help of an example.

Ans. Access specifiers are used to control the accessibility of data members and
member functions of class. It helps classes to prevent unwanted exposure of
members to outside world.

5. Compare multiple inheritance with multilevel inheritance and hierarchical


inheritance.

Structure & Class

1. How is a structure in C++ different from a class? Explain with the help of example.

2. Compare structures and classes in C++. What are empty classes? Explain the
purpose of empty classes.

3. What are static members of a class? What is the utility of having static members?
Explain with the help of an example.

4. Write a program in C++ to define a class ‘‘Teacher’’ with a virtual function ‘‘Salary’’.
Derive the class ‘‘Assistant Professor’’ from the class ‘‘Teacher’’ and implement the
salary function. Make necessary assumptions.
5. “Abstract class provides a base, upon which other classes may be built.” Justify the
statement, with the help of an example.

Object

1. What is an object in C++?


Explain, how an object can be passed as an argument to a function with the help of
an example.

Friend Function & Member Function

1. What is friend function? Explain its advantages with the help of an example.
Briefly discuss the properties of a friend function.
Write a program in C++ to illustrate the concept of friend function.

2. How does Friend Function differ from the member function?

3. What is a Static Member Function? Write a program in C++ to illustrate the concept
of the static member function.

4. What is a member function? Briefly discuss the purpose of member function in C++,
with suitable example.

Inline Function

1. What is an inline function?

2. Explain the advantages of inline function, with suitable example.

3. How does the execution of inline functions differ from normal functions?
Give the advantages of inline functions.
Exception Handling

1. What is exception?

2. Explain, how exception handling is done in C++, with the help of a program.

3. Also discuss, what will happen if an exception is thrown outside of a try block.

4. Briefly discuss the functioning of Try, Throw and Catch expressions with suitable
block diagram.

Function overloading & overriding

1. What is function overloading? How is it different from function overriding? Explain


with an example C++ program for each.

2. Explain, how function calls are matched in a C++ program in which functions are
overloaded. Use appropriate example program for your explanation.

3. How are function calls matched with overloaded functions? Explain with the help of
an example.

4. What is operator overloading? Why some operators can’t be overloaded? Write a


program to overload ‘+’ operator to add two complex numbers.

5. What are the limitations of Operator overloading and Function overloading.


Virtual Function

1. What is virtual function?

2. Write a program in C++ to create a class Doctor with a virtual function salary.
Derive a class visiting Doctor and implement function salary in it.

3. Virtual Functions and their limitations

4. Compare virtual functions and pure virtual functions. Explain with the help of an
example.

Access Control Specifier

1. What is Access Specifier? Explain different access specifiers in C++.

2. Short note on Access control specifier

Stream Manipulator

1. What are Stream Manipulators? Briefly discuss the purpose of various stream
manipulators.

2. Explain the use of setw( ) and setprecision( ) as a stream manipulator.

3. What is stream in C++? Name the streams generally used for file I/O.

Message Passing
1. Briefly discuss the term Message Passing.

2. How does message passing support the concept of interfaces in C++? Explain with
the help of an example.

Polymorphism

1. What is Polymorphism? What are the advantages of polymorphism? Mention the


types of polymorphism supported by C++.

2. Compare Compile-time Polymorphism with Run-time Polymorphism.

3. Explain the association of dynamic binding and runtime polymorphism, with


suitable example.

Type Convertion

1. What is Type Conversion? What is the advantage of Type Conversion?

2. Briefly discuss Type Casting and Automatic Type Conversion.

Miscellaneous

1. Short note on Scope Resolution Operator

2. What is STL? Briefly discuss the components of STL.


3. Discuss the taxonomy of C++ data types with the help of a suitable block diagram.

4. What do you understand by the signature of a method? Briefly discuss the


components of the signature of a method.

5. Discuss the role of “new” and “delete” as memory management operations.

6. Short note on File Stream Operations

7. What is ‘this’ pointer? Explain the significance of ‘this’ pointer with the help of an
example program.

8. What are static data members of a class? Explain the characteristics of static data
members.

9. Compare Early Binding and Late Binding. Explain when to use which type of binding.

10. What are containers? Explain the use of list container class with the help of an
example.

11. What is Encapsulation? Are encapsulation and information hiding the same?
Explain.
Basics

1. Compare Structured programming with Object-oriented programming. Give two


advantages of both.

2. What are the essential properties of object oriented programming?

3. Differentiate between C and C++, give at least five differences.

4. Compare Break and Continue statement. Exhibit the usage of break and continue
statement with suitable code in C++.

5. What are Breaking Statements? Give syntax of the following breaking statements :
(i) break (ii) continue (iii) goto (iv) exit
6. Explain the use of ‘&&’ and ‘!’ operators in C++ with the help of an example.

7. What do you understand by scope of a variable? Compare global variable and local
variable in C++.

8. Differentiate between the following :


(i) Binary file and Text file
(ii) get( ) and getline( )

9. What is the difference between a keyword and an identifier? Explain with the help
of an example.

10. What is the difference between call by value and call by reference in a user defined
function in C++? Give an example to illustrate the difference.

11. Short note on Relational operators in C++

Programming

1. Write a C++ program to find the average three given numbers. Define appropriate
class and methods in the program.

2. Write a C++ program to implement simple calculator to perform ‘+’, ‘–’, ‘*’, ‘/’ on
two operands. Your program should have methods for reading data and for
performing arithmetic operations.

3. Write a program in C++ to open an existing file and insert the text ‘‘File program in
C++’’ at the end of the file. Your program should have suitable comments.

You might also like