BCS-031 - C++ Programming
BCS-031 - C++ Programming
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:
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
Eg1:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class student
{
int roll;
char name[30];
public:
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();
}
#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:
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.
1. What is function template? Write a function template SUM to add two numbers.
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++
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";
};
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.
#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;
}
};
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
#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.
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.
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 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.
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
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.
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.
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.
4. Compare virtual functions and pure virtual functions. Explain with the help of an
example.
Stream Manipulator
1. What are Stream Manipulators? Briefly discuss the purpose of various stream
manipulators.
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
Type Convertion
Miscellaneous
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
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++.
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.
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.