OOP 2 Marks N Prog PDF
OOP 2 Marks N Prog PDF
OOP 2 Marks N Prog PDF
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.
4. Define Inheritance.
It is process by which a class inherits property from other class.
The class who has borrowed/ inherited property is called derived class.
The class from whom the property is inherited is called base class.
Inheritance is used by ‘:’ symbol.
Syntax :
};
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.
Private
Public
Protected
Example :
int m=1920, n=5;
cout<<setw(6)<<m;
cout<<setw(6)<<n;
Output:
1920
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 :
Example :
int m=19.12345787, n=5.15478;
cout<< setprecision (3)<<m;
cout<< setprecision (4)<<n;
Output:
19.123
5.1549
10. Explain setfill() and getline() function.
(1) setfill() is used to fill empty field width provided in setw() function.
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 doesn’t 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
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
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
Endl \n
Syntax : #include<file.h>
Example:
#include<iostream.h>
19. Define manipulators and also mention the manipulators that are used in C++
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.
Inheritance Polymorphism
When a class borrows properties
Polymorphism means one name, many
1 from other class, this process is 1
forms.
called inheritance
Inherits properties from Same function name or operator can
2 2
base/parent class be used for different execution purpose
Some function of base class can be Function has same name but different
3 3
used in different derived classes implementation in different class.
No pre requirement for this to Can exist when inheritance is
4 4
happen available.
5 types: 2 types:
1) Single 1) Compile time
2) multiple a) function overloading
5 5
3) multilevel b) operator overloading
4) hierarchical 2) run time
5) hybird a) virtual function
Example:
#include<iostream.h>
int main()
{
/* Multi line comment */
// single line comment
cout<< “Hello”;
getch();
retun 0;
}
25. List advantages of an object oriented programming language (x2)
1) user defined data types can easy be created
2) data security with the help of encapsulation.
3) Existing code can be reused by means of inheritance.
4) Better maintainability of code
5) Better quality of software & less maintenance cost
6) Can easily be upgraded from small to large systems.
7) Easy to partition the work
8) Software complexity can easily managed.
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.
Constructor
Implicit Explicit
Constructor Constructor
Default Copy
Constructor Constructor
5. Type Casting
#include<iostream.h> OUTPUT:
#include<conio.h>
int main() Division= 8.333333
{
int a=25,b=3;
float c;
c=float(a)/b;
cout<<”Division= “<<c;
getch();
return 0;
}
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() OUTPUT:
{
abc obj; Addition= 15
obj.add();
getch();
return 0;
}
8. Array Of Objects:
#include<iostream.h>
#include<conio.h>
class abc
{
private:
int x;
public:
void f1()
OUTPUT:
{
x=5
x=5;
cout<<"x= "<<x<<endl; 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() OUTPUT:
{ 5
int x, y; 10
cin>>x>>y; Addition= 15
abc *p=new abc;
p->f1(x, y);
getch();
return 0;
}
10. Recursive Function:-
#include<iostream.h>
#include<conio.h>
int factorial(int no)
{
if(no>1)
{
return(no*factorial(no-1));
}
else
OUTPUT:-
{
5
return 1;
Factorial: 120
}
}
int main()
{
int no;
cin>>no;
cout<<"Factorial: "<<factorial(no);
getch();
return 0;
}
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;
OUTPUT:
cin>>a>>b;
Enter two nos.:
A obj1;
5
B obj2;
10
obj1.enter_a(a);
obj2.enter_b(b); Addition: 15
cout<<"Addition: "<<add(obj1,obj2);
getch();
return 0;
}
class B
{
protected:
int b;
public:
void get_b()
{
b=10;
}
};
class A
{
protected:
int a;
public:
void set_a()
{
a=5;
}
};
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 B
{
protected:
int c;
public:
void set_c()
{
c=2;
}
void add_abc()
{
cout<<"a+b+c= "<<a+b+c<<endl;
}
};
int main()
{ OUTPUT:
C obj; a+b= 15
obj.set_a(); a+b+c= 17
obj.set_b();
obj.set_c();
obj.add_ab();
obj.add_abc();
getch();
return 0;
}
class C : public A
{
protected:
int c;
public:
void set_c()
{
c=10;
}
void add_ac()
{
cout<<"a+c= "<<a+c<<endl;
}
};
class B : public A
{
public:
void f1()
{
cout<<"B Class...";
}
};
int main()
{
B obj; OUTPUT:
obj.f1(); B Class...
getch();
return 0;
}
24. Operator Overloading (binary operator):
#include<iostream.h>
#include<conio.h>
class abc
{
private :
int x,y;
public:
void set_xy()
{
x=5;
y=10;
}
void operator=(abcobj)
{
x=obj.x;
y=obj.y;
}
void display()
{
cout<<"Value of x: "<<x<<endl;
cout<<"Value of y: "<<y<<endl;
}
};
int main()
OUTPUT:
{
Value of x= 5
abc obj1,obj2;
Value of y= 10
obj1.set_xy();
obj2=obj1;
obj2.display();
getch();
return 0;
}
25. Pure Virtual Function:
#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void show()=0;
};
class B : public A
{
public:
void show()
{
cout<<"Implementation of Virtual Class...";
}
};
int main()
{ OUTPUT:
A *obj1; Implementation of Virtual Class...
B obj2; ---(hierarchical inheritance)---
obj1=&obj2;
obj1->show();
getch();
return 0;
}
};
int main() OUTPUT:
{ 5
abc obj,obj(15); 15
getch();
return 0;
}
28. Pointer To Derived Class:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
void f1()
{
x = 5;
cout<<x;
}
};
class B : public A
{
public:
int y;
void f2()
{
y = 10;
cout<<y;
}
};
int main() OUTPUT:
{ 10
B *p = new B;
p -> f2();
getch();
return 0;
}