Académique Documents
Professionnel Documents
Culture Documents
2 Marks Questions
1. Define Class.
Class is a collection of objects of similar type.
It is a blue print of the real world objects.
Class is a description of objects behavior i.e. it describes objects.
Syntax :
class class_name
{
//body of the class
};
Example :
class abc
{
//body of class
};
2. Define Object.
An instance of a class is called objects. It is run time identifiable entity which
represents characteristics & behavior of class.
Syntax : class_name object_name;
Example:
class_name obj1;
class_name obj1,obj2;
Example:
class derived : public class base
{
};
5. Define Polymorphism.
Polymorphism means same name different forms.
Using this, we can define several operations with same name for different things.
Polymorphism is essential feature of OOP
Types of polymorphism
6. Define Encapsulation.
The wrapping up of data & function into a single unit called class is known as
encapsulation.
The data is accessible only to the functions, which are wrapped in the class.
It done by access modifiers i.e. public, private, protected, friend.
7. Define Dynamic Binding.
Dynamic binding means the linking of object to properties & methods occur at run
time.
It is also known as late binding.
Same name can be used for similar methods on different kinds of objects.
8. What is visibility mode? List different visibility modes supported by C++.
Visibility mode is a mechanism available in OOP & C++ that enables the access of
particular data member & member function of class.
Using visibility modes, one can allow or restrict the access of data & methods of
class.
It is used in inheritance as well in other programming execution in C++ where only
particular methods & properties are allowed for access.
Private
Public
Protected
Example :
int m=1920, n=5;
cout<<setw(6)<<m;
cout<<setw(6)<<n;
Output:
1920
5
2) setprecision() is used to set precision after decimal point in C++ output screen.
It is same as precision() function.
Accepts one integer argument i.e. precision point after decimal.
Syntax :
cout<< setprecision(precision point)<<data_member;
Example :
int m=19.12345787, n=5.15478;
cout<< setprecision (3)<<m;
cout<< setprecision (4)<<n;
Output:
19.123
5.1549
Accepts one character argument & fills the empty filed space with that character
Output:
*********5
(2) getline() operates like get() & inserts a null character \0 after the line in the
character array.
Removes delimiter from stream but doesnt store in buffer
Example:
char msg[30];
cout<< type message : ;
cin.getline(msg,30);
cout<<msg;
Output:
Type message : C++
C++
A stream is continues flow of character/data. All input & output is performed with
stream.
Sequence of characters organized into lines.
Each line has zero or more characters
Ends with new line character
C++ stream classes:
1. ISTREAM input stream
2. OSTREAM output stream
3. IOSTREAM input output stream
4. STREAMBUF buffered stream
Scope resolution operator is used to solve conflicts of scope in terms of data member
as well as methods.
Symbolized with ::
It is also used to access global variables, as well as to solve conflicts of access to
function in hybrid inheritance. Function call be defined outside of class using ::
Example:
int a=25;
int main()
{
int a=10;
cout<<a;
cout<<::a;
getch();
return 0;
}
Output:
10
25
13.
Define constructor
Constructor is a special member function of class, having same name as class
It doesnt have any return type
When a class name is return with () in class, constructor is created.
Must be declared in public section.
Syntax:
class class_name
{
public:
class_name()
{
//constructor of class
}
}
Example:
class abc
{
public:
abc()
{
//constructor of class
}
}
Example:
class abc
{
public:
virtual void show() = 0; //pure virtual function
}
When a variable is declared outside the scope of class & main() function, it is said to
be global variable.
Global variables can be accessed by any class or main() function.
But is a class or main() function contains same name for other variable, then there
will be a conflict of scope.
To resolve this conflict, we use scope resolution operator ::
Syntax:
Header files
data-type variable-name = value;
Example:
#include<iostream.h>
int a=25;
int main()
{
int a=10;
cout<<a;
cout<<::a;
getch();
return 0;
}
Output:
10
25
17.
\n
1
Escape sequence
Alternative if-else :
if(m>n)
{
m;
}
else
{
n;
}
Syntax : #include<file.h>
Example:
#include<iostream.h>
19. Define manipulators and also mention the manipulators that are used in C++
20.
Example:
cout<<width(5)<< C++;
cout<<width(7)<< C++;
Output:
C++
C++
precision() is used to set precision after decimal point in C++ output screen.
It is same as setprecision() function.
Accepts one integer argument i.e. precision point after decimal.
Syntax : cout<< precision(precision point)<<data_member;
Example :
int m=19.12345787, n=5.15478;
cout<< precision (3)<<m;
cout<< precision (4)<<n;
Output:
19.123
5.1549
Syntax : cout.fill(Character);
Example :
int a=5;
cout.width(5);
cout.fill(*);
cout<<a;
Output:
****5
setf() function is used to specify format flags that can control the form of output
display. Like left or right justification.
Syntax : cout.setf(arg1, arg2);
Example:
char a[20]= c++;
cout.setf(ios::fixed, ios::floatfield);
cout<<a;
22. Explain the role of abstract class while building a class Hierarchy.
OR
What is use of abstract class?
Abstract class is a class which provides base for derived classes in inheritance. When
building class hierarchy i.e. hierarchical inheritance, each class may have different
implementation for a particular function for each derived classes.
To overcome this situation, base class must provide a base where function of a base
class can have different implementation in derived classes.
This can be achieved by providing virtual function in base class. This way abstract
class plays important role in building class hierarchy.
23. Differentiate between the following terms:
a. Data abstraction and Data encapsulation
Data abstraction
It is act of representing important
1 features without giving its
explanations
Data encapsulation
1
Polymorphism
1
2 types:
1) Compile time
a) function overloading
b) operator overloading
2) run time
a) virtual function
5 types:
1) Single
2) multiple
5
3) multilevel
4) hierarchical
5) hybird
24. Write the structure of C++ program
Header file
Global variable declaration //optional
Class definition
//optional
Main body of program
{
//scope & code statements
}
Example:
#include<iostream.h>
int main()
{
/* Multi line comment */
// single line comment
cout<< Hello;
getch();
retun 0;
}
Inline function is replaced or expanded in the place from where it is called at the
function call time.
This reduces or eliminates the overhead stated above.
So the purpose of inline function is to reduce the calling overhead when there is need
of calling it very often.
Abstract Class: When a class is only used to provide base to its derived class in
inheritance, that base is called abstract.
It only states main features like function declaration & not its explanation
(implementation).
When a class has minimum one pure virtual function, it acts as abstract class.
Pure virtual function: when a member function of a base class is initialized with 0 &
is made virtual, it is called pure virtual function. It is used to make abstract base
class.
Object is an identifiable run time entity with some characteristics & behavior. It is an
instance of the class.
Data Types
User Defined
Data Types
structure, union,
class, enum
Derived Data
Types
array, function,
pointer reference
Implicit
Constructor
Default
Constructor
Explicit
Constructor
Copy
Constructor
No Argument
Parameterized
Copy
Constructor
Dynamic
Constructor
1. Reference variable
#include<iostream.h>
#include<conio.h>
int main()
{
int a;
cin>>a;
int &r=a;
r=r+10;
cout<< r= <<r<<endl;
cout<< a= <<a<<endl;
getch();
return 0;
}
C++ Programs
OUTPUT:
4
r= 40
a=40
OUTPUT:
Local a: 256
Global a:128
OUTPUT:
Division= 8.333333
6. Access Modifiers:
#include<iostream.h>
#include<conio.h>
class abc
{
private:
int x;
protected:
int y;
public:
void add()
{
x=5;
y=10;
int z = x + y;
cout<<"Addition= "<<z<<endl;
}
};
int main()
{
abc obj;
obj.add();
getch();
return 0;
}
OUTPUT:
Addition= 15
public:
void f1()
{
x=5;
cout<<"x= "<<x<<endl;
}
OUTPUT:
x=5
x=5
};
int main()
{
abc obj[2];
for(int i=0;i<=2;i++)
{
obj[i].f1();
}
getch();
return 0;
}
9. 'this' Keyword:
#include<iostream.h>
#include<conio.h>
class abc
{
private :
int x, y;
public:
void f1(int x, int y)
{
int c;
this->x=x;
this->y=y;
c=this->x+ this->y;
cout<<"Addtion= " <<c<<endl;
}
};
int main()
{
int x, y;
cin>>x>>y;
abc *p=new abc;
p->f1(x, y);
getch();
return 0;
}
OUTPUT:
5
10
Addition= 15
OUTPUT:5
Factorial: 120
int main()
{
int x,y;
cin>>x>>y;
cout<<"Call by value swap..."<<endl;
abc obj;
obj.call_val(x,y);
cout<<x<<endl<<y<<endl;
cout<<"Call by reference swap..."<<endl;
obj.call_ref(x,y);
cout<<x<<endl<<y<<endl;
getch();
return 0;
}
OUTPUT:
10
5
Call by value swap...
10
5
Call by reference swap...
5
10
OUTPUT:
Enter no.: 5
Cube: 125
{
x=a;
}
};
class B
{
private:
int y;
public:
friend int add(A obj1, B obj2);
void enter_b(int b)
{
y=b;
}
};
int add(A obj1,B obj2)
{
int z;
z=(obj1.x+obj2.y);
return z;
}
int main()
{
int a,b;
cout<<"Enter two nos.: "<<endl;
cin>>a>>b;
A obj1;
B obj2;
obj1.enter_a(a);
obj2.enter_b(b);
cout<<"Addition: "<<add(obj1,obj2);
getch();
return 0;
}
14. Default Argument:
#include<iostream.h>
#include<conio.h>
int f1(int a, int b=5)
{
return (a+b);
}
OUTPUT:
Enter two nos.:
5
10
Addition: 15
int main()
OUTPUT:
{
30
cout<<f1(10,20)<<endl;
15
cout<<f1(10)<<endl;
getch();
return 0;
}
15. Function Overloading:
#include<iostream.h>
#include<conio.h>
class abc
{
public:
void calc(int a, int b)
{
int c=a+b;
cout<<"Sum: "<<c<<endl;
}
void calc(int a, float b)
{
int c=a/b;
cout<<"Division: "<<c<<endl;
}
void calc(int a)
{
if(a>0)
cout<<"Positive no."<<endl;
}
};
int main()
OUTPUT:
{
10
int a, b;
5
cin>>a>>b;
Sum: 15
abc obj1;
Division: 2
obj1.calc(a, b);
obj1.calc(a, float(b));
Positive
obj1.calc(a);
getch();
return 0;
}
OUTPUT:
Copy constructor called...
OUTPUT:
15
getch();
return 0;
}
19. Multiple Inheritance :
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void get_a()
{
a=5;
}
};
class B
{
protected:
int b;
public:
void get_b()
{
b=10;
}
};
class C : public A, public B
{
int c;
public:
void add()
{
c=a+b;
cout<<c;
}
};
int main()
{
C obj;
obj.get_a();
obj.get_b();
obj.add();
getch();
return 0;
}
OUTPUT:
15
class C : public B
{
protected:
int c;
public:
void set_c()
{
c=2;
}
void add_abc()
{
cout<<"a+b+c= "<<a+b+c<<endl;
}
};
int main()
{
C obj;
obj.set_a();
obj.set_b();
obj.set_c();
obj.add_ab();
obj.add_abc();
getch();
return 0;
}
21. Hybrid Inheritance:
#include<iostream.h>
#include<conio.h>
class A
{
protected:
int a;
public:
void set_a()
{
a=5;
}
};
OUTPUT:
a+b= 15
a+b+c= 17
class B:public A
{
protected :
int b;
public:
void set_b()
{
b=10;
}
void add_ab()
{
cout<<"a+b= "<<a+b<<endl;
}
};
class C : public A
{
protected:
int c;
public:
void set_c()
{
c=10;
}
void add_ac()
{
cout<<"a+c= "<<a+c<<endl;
}
};
class D: public B, public C
{
protected:
int d;
public:
void set_d()
{
d=5;
}
void add_abcd()
{
cout<<"a+b+c+d= "<<B::a+b+c+d<<endl;
}
};
int main()
{
D obj;
obj.B::set_a();
obj.set_b();
obj.set_c();
obj.set_d();
obj.add_abcd();
getch();
return 0;
}
OUTPUT:
a+b= 15
a+c= 15
a+b+c+d=30
int main()
{
B objb;
C objc;
objc.set_ab();
objc.mul_ab();
objb.add_ab();
getch();
return 0;
}
OUTPUT:
a+b= 20
a*b= 75
OUTPUT:
B Class...
OUTPUT:
Value of x= 5
Value of y= 10
int main()
{
A::B obj;
obj.f1(5);
getch();
return 0;
}
OUTPUT:
x=5
OUTPUT:
5
15
OUTPUT:
10