Constructor
Constructor
Default:
Default constructor is the constructor which doesn't take any argument. It has
no parameter.A default constructor is so important for initialization of object
members, that even if we do not define a constructor explicitly, the compiler
will provide a default constructor implicitly.
Example:
class c
public:
int s;
c()
s = 10;
};
int main()
c c1;
return 0;
Parameterized:
These are the constructors with parameters.
Example:
class c
public:
int s;
c(int a)
s = a;
};
int main()
c c1(10);
c c2(37);
return 0;
Copy:
These are special type of Constructors which takes an object as argument,
and is used to copy values of data members of one object into another object.
Example:
class Stud
public:
int rollno;
string name;
Stud(int a)
rollno = a;
name = "";
}
Stud(int a, string s)
rollno = a;
name = s;
};
int main()
Stud A(10);
return 0;
#include<iostream>
class abc{
int a,b;
int p(int a,int b){ //p is a private member function can only be
accessed within the class
int c=a+b;
cout<<"Sum is:"<<c;
public:
int display(){
p(1,2);
};
int main(){
abc d;
d.display();
return(0);}
Output:
Sum is:3
Private Public
If data are declared as private The member functions with
in a class then it is accessible public access specifiers can
by the member functions of the be accessed outside of the
class where they are declared. class.
Compile Time:
This type of polymorphism is achieved by function overloading or operator
overloading.
Function Overloading:
When there are multiple functions with same name but different parameters
then these functions are said to be overloaded
Runtime polymorphism:
It is also known as dynamic polymorphism or late binding.
In runtime polymorphism, the function call is resolved at run time.
In contrast, to compile time or static polymorphism, the compiler deduces the
object at run time and then decides which function call to bind to the object.
Ques 24 Differentiate between virtual and pure virtual function.
Answer:
Virtual Function Pure Virtual Function
Member function declared
within the base class that
A virtual function in the base
can be redefined or
class with no implementation
overridden by a derived
class
A derived class that does A derived class that does not
not override a virtual override a pure virtual
function of the base class function of the base class
does not cause any causes any compilation
compilation errors. errors.
Base class does not have
Base class has the function
the function definition,it only
definition.
has function declaration.
There is at least one pure
There is no abstract class
virtual function,that class is
concept.
an abstract class.
Declaration:
Declaration: virtual get(int a)=0;
get(int a); // =0 is known as a pure
specifier.
#include <iostream>
using namespace std;
void swapValue(int *a,int *b);
int main(){
int a = 100;
int b= 200;
printf("First number is %d\n",a);
printf("Second Number is %d\n",b);
swapValue(&a,&b);
printf("After swap function \n");
printf("%d\n%d",a,b);
}
void swapValue(int *x,int *y){
int temp;
temp = *x;
*x=*y;
*y=temp;
}
Ques 15 What are protected member? What is difference between public and
protected access modifier?
Answer:
Protected Member:
The protected keyword specifies access to class members in the member-list
up to the next access specifier or the end of the class definition.
A protected member variable or function is very similar to a private member
but it provides one additional benefit that they can be accessed in child
classes which are called derived classes.
Difference between Public and Protected access modifier:
#include <iostream>
#include <string>
class item
public:
float oosd;
int dbms,cd,daa;
void putdata(void);
};
oosd=a;
dbms=b;
void item::putdata()
cout<<"DBMS: "<<dbms<<endl;
cout<<"OOSD: "<<oosd<<endl;
int main()
item i;
i.getdata(30.0,20);
i.putdata();
return 0;
}
Ques 2 Design a class using C++ to create a singly linked list.
Answer:
Program
#include <iostream>
struct node
int data;
node *next;
};
class linked_list
private:
node *head,*tail;
public:
linked_list()
head = NULL;
tail = NULL;
void add_node(int n)
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
head = tmp;
tail = tmp;
}
else
tail->next = tmp;
tail = tail->next;
};
int main()
linked_list a;
a.add_node(1);
a.add_node(2);
return 0;
Implicit Conversion:
When the user manually changes data from one type to another, this is
known as explicit conversion, also known as type casting.
Here the user can typecast the result to make it of a particular data
type.
There are three major ways in which we can use explicit conversion in
C++.
scope operator
sizeof
member selector
member pointer selector
ternary operator
Ques 11 What are the rules of operator overloading?
Answer:
Rules for operator overloading in C++
class A
{
public:
~A()
// statement
};
Properties of Destructor:
class s {
public:
return a;
};
Ques 23 Explain this pointer with a help of example.
Answer:
The this pointer holds the address of the current object, in simple words you
can say that this pointer points to the current object of the class.
Example:
#include <iostream>
class Pointer{
private:
int n;
char c;
public:
this->n=n;
this->c=c;
void display(){
cout<<"Number:"<<n<<endl;
cout<<"Character:"<<c;
};
int main(){
Pointer p1;
p1.get(1, 'C');
p1.display();
return 0;
}
Ques 17 Write a program in C++ to read in two matrices from the keyboard
and compute their sum.
Answer:
#include <iostream>
int main()
cin >> r;
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cout << endl << "Enter elements of 2nd matrix: " << endl;
cout << "Enter element b" << i + 1 << j + 1 << " : ";
if(j == c - 1)
return 0;
class Bat {
public: int b;
};
};
};
};
int main() {
Cow obj;
obj.d1 = 60;
obj.d2 = 70;
obj.d3 = 80;
class Animal {
public:
void eat(){
cout<<"Eating...";
};
public:
void eat()
cout<<"Eating bread...";
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;