Beruflich Dokumente
Kultur Dokumente
PART-A 2 Marks
1. Define Constructor.
A constructor is defined as a special member of a class, which has no
return type. It can
be used to initializes an object immediately upon creation.
11. What are the conditions to satisfy the type casting function?
The conditions to satisfy the type casting function are,
i. It must be a class member.
ii. It must not specify a return type.
iii. It must not have any arguments.
PART-B
16-MARKS QUESTIONS WITH ANSWERS
1. Explain the various types of constructors that are available in C++ with
Suitable examples.
Constructors
Constructors are special class functions which performs initialization of every
object. The Compiler calls the Constructor whenever an object is created. Constructors
initialize values to object members after storage is allocated to the object. While defining
a constructor you must remember that the name of constructor will be same as the name
of the class, and contructors never have return type.
Syntax:
class A// Class name A
{
int x;
public:
A(); //Constructor name A
};
Types of Constructors
Constructors are of three types:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Dynamic Constructor
1. Default Constructor
Syntax:
class_name() {
-----
-----
}
Example Program:
#include<iostream>
using namespace std;
class Marks
{
public:
int maths;
int science;
//Default Constructor
Marks()
{
maths=0;
science=0;
}
display() {
cout << "Maths : " << maths <<endl;
cout << "Science :" << science << endl;
}
};
int main() {
//invoke Default Constructor
Marks m;
m.display();
return 0;
}
Output:
Maths: 0
Science: 0
2. Parameterized Constructor
Syntax:
class_name(Argument_List)
{
-----
-----
}
Example Program:
#include<iostream>
using namespace std;
class Marks
{
public:
int maths;
int science;
Marks(int mark1,int mark2) //Parametrized Constructor
{
maths = mark1;
science = mark2;
}
display() {
cout << "Maths : " << maths <<endl;
cout << "Science :" << science << endl;
}
};
int main() {
//invoke Parametrized Constructor
Marks m(90,85);
m.display();
return 0;
}
Output
Maths : 90
Science : 85
3. Copy Constructor
Copy constructor is a special type of constructor in which new object is created is
copy of existing object.
Syntax:
class_Name (const class_Name &obj)
{
// body of constructor
}
Example Program:
#include<iostream>
using namespace std;
class marks
{
public:
int maths;
int science;
//Default Constructor
marks()
{
maths=0;
science=0;
}
//Copy Constructor
marks(const marks &obj)
{
maths=obj.maths;
science=obj.science;
}
display(){
cout<<"Maths : " << maths
cout<<"Science : " << science;
}
};
int main(){
marks m1;
marks m2(const marks &m1);
m2.display();
return 0;
}
Output
Maths : 0
Science : 0
4. Dynamic Constructor
Dynamic constructor is used to allocate the memory to the objects at the run time.
Memory is allocated at run time with the help of 'new' operator. By using this constructor,
we can dynamically initialize the objects.
Output:
The destructor is called when the object is destroyed. The object can be destroyed
automatically when the scope of the objects end or explicitly by using operator delete. The
destructor must have the same name as the class, but proceeded with a tilde sign (~) and it
must also return no value.
3. What is meant by polymorphism? Explain the various types of
polymorphism in C++ with suitable examples.
Polymorphism is the ability to use an operator or function in different ways.
Polymorphism gives different meanings or functions to the operators or functions. Poly,
referring too many, signifies the many uses of these operators and functions. A single
function usage or an operator functioning in many ways can be called polymorphism.
Polymorphism refers to codes, operations or objects that behave differently in different
contexts.
Types of Polymorphism:
C++ provides two different types of polymorphism.
run-time
compile-time
Run-time:
The appropriate member function could be selected while the programming is
running. This is known as run-time polymorphism. The run-time polymorphism is
implemented with inheritance and virtual functions.
Virtual functions:
A function qualified by the virtual keyword. When a virtual function is called via a
pointer, the class of the object pointed to determine which function definition will be used.
Virtual functions implement polymorphism, whereby objects belonging to different
classes can respond to the same message in different ways.
Virtual Function in C++
A virtual function is a member function of class that is declared within a base class and re-
defined in derived class.
When you want to use same function name in both the base and derived class, then
the function in base class is declared as virtual by using the virtual keyword and again re-
defined this function in derived class without using virtual keyword.
Syntax
Example:
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void show()
{
cout<<"Hello base class";
}
};
class B : public A
{
public:
void show()
{
cout<<"Hello derive class";
}
};
void main()
{
clrsct();
A aobj;
B bobj;
A *bptr;
bptr=&aobj;
bptr->show(); // call base class function
bptr=&bobj;
bptr->show(); // call derive class function
getch();
}
Output
Compile-time:
The compiler is able to select the appropriate function for a particular call at
compile-time itself. This is known as compile-time polymorphism. The compile-time
polymorphism is implemented with templates.
Ex:
Operator overloading
Function overloading
1. Operator Overloading
1. Create a class that defines the data type that is to be used in the overloading
operation.
2. Declare the operator function operator op () in the public part of the class. It
may be either a member function or a friend function.
3. Define the operator function to implement the required operations.
Example Program: Binary Operator Over loading
#include<iostream.h>
#include<conio.h>
class overloading
{
int value;
public:
void setValue(int temp)
{
value = temp;
}
overloading operator+(overloading ob)
{
overloading t;
t.value=value+ob.value;
return(t);
}
void display()
{
cout<<value<<endl;
}
};
int main()
{
overloading obj1,obj2,result;
int a,b;
result = obj1+obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
getch();
return 0;
}
Output:
Function overloading is a concept in which one can use many functions having
same function name but can pass different number of parameters or different types of
parameters.
There are two types of polymorphism in C++. Compile time polymorphism and
runtime polymorphism. Function overloading comes under the Compile time
polymorphism.
Example:
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
Output:
30
60
Example:
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
Output:
30
60