Oop Pop
Oop Pop
The main program is divided into The main program is divided into
Programs small object depending on the small parts depending on the
problem. functions.
OR
1. Abstraction
Abstraction is a process of exposing essential features of an entity by hiding the other irrelevant details.
Abstraction mainly reduces complexity and increases efficiency. An entity can have multiple
abstractions.
2. Encapsulation
Encapsulation is the process of putting data and the operations (functions) that can be performed on
that data into a single container called class. Now, any programmer can use this class without knowing
how it is implemented. Due to encapsulation, data is insulated, thus not directly accessible from the
outside world. This is known as Data Hiding.
Remember, Encapsulation is not data hiding, but, Encapsulation leads to data hiding.
3. Inheritance
In a nutshell, inheritance is a process of creating new class from the existing one. The new class may
have some additional properties and functionalitiy.
4. Polymorphism
Poly means many and Morphs means forms. Polymorphism refers to the ability to take multiple forms
and it allows us to invoke derived class methods through a base class reference during run-time.
3. Difference/Characteristc of Constructor and Destructor(with example)
Example of constructor:
class A
{
int x;
public:
A(); //Constructor
};
Constructors can be defined either inside the class definition or outside class definition using class name
and scope resolution :: operator.
class A
{
int i;
public:
A(); //Constructor declared
};
Example:
class A
{
public:
~A();
};
2) In Multilevel Inheritance a Derived class can also inherited by another class Means in this Whena
Derived Class again will be inherited by another Class then it creates a Multiple Levels.
3) Multiple Inheritances is that in which a Class inherits the features from two Base Classes When a
Derived Class takes Features from two Base Classes.
4) Hierarchical Inheritance is that in which a Base Class has Many Sub Classes or When a Base Class is
used or inherited by many Sub
Classes.
5) Hybrid Inheritance: - This is a Mixture of two or More Inheritance and in this Inheritance a Code
May Contains two or Three types of inheritance in Single Code.
5. Write a program for unary operator overloading.
ALGORITHM:(for understanding)
PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b,c;
public:
complex(){}
void getvalue()
{
cout<<"Enter the Two Numbers:";
cin>>a>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void operator--()
{
a=--a;
b=--b;
}
void display()
{
cout<<a<<"+\t"<<b<<"i"<<endl;
}
};
void main()
{
clrscr();
complex obj;
obj.getvalue();
obj++;
cout<<"Increment Complex Number\n";
obj.display();
obj--;
cout<<"Decrement Complex Number\n";
obj.display();
getch();
}
Output:
Enter the two numbers: 3 6
Increment Complex Number
4+ 7i
Decrement Complex Number
3+ 6i
body of function
}
Program:
#include <iostream>
#include <conio.h>
int main()
int i, N;
cin>>N;
for(i=1;i<=N;i++)
print(i);
}
getch();
return 0;
Output
C++ Program to print first N natural numbers
10
1 2 3 4 5 6 7 8 9 10
int main()
{
char first[] = "C programming";
char second[] = "C++ programming";
display(first);
display(first, second);
return 0;
}
Output of program:
C programming
C programming
C++ programming
1. endl
The endl manipulator works the same way as the \n character in C++. That is the endl manipulator
outputs the subsequent data or text in the next line.
But the difference is that endl also flushes the output buffer when it is used in a program.
<#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num;
cout<<"Enter your roll number: ";
cin>>num;
setw manipulator:
This manipulator is used to set the width of the output in a program. It takes up an argument n
which is the width of the field in which the output is to be displayed.
Example:
#include<iostream>
#include<conio.h>
#include<iomanip>
int main()
int num1,num2,num3;
cout<<"Enter three numbers:\n";
cin>>num1>>num2>>num3;
<<"Num1:"<<setw(8)<<num1<<endl
<<"Num2:"<<setw(8)<<num2<<endl
<<"Num3:"<<setw(8)<<num3<<endl;
getch();
return 0;
Output:
Behaves as if member fill were called with c as argument on the stream on which it is inserted as a
manipulator (it can be inserted on output streams).
// setfill example
#include <iostream> // std::cout, std::endl
#include <iomanip> // std::setfill, std::setw
int main () {
std::cout << std::setfill ('x') << std::setw (10);
std::cout << 77 << std::endl;
return 0;
}
Output:
xxxxxxxx77
Sets the decimal precision to be used to format floating-point values on output operations.
Behaves as if member precision were called with n as argument on the stream on which it is
inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams).
int main () {
double f =3.14159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
std::cout << std::fixed;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
return 0;
}
Output:
3.1416
3.14159
3.14159
3.141590000
10. Define Operator overloading with a sample program.
Operator Overloading
Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator
is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on
user-defined data type. For example '+' operator can be overloaded to perform addition on various data
types, like for Integer, String(concatenation) etc.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator ()to subtract two complex numbers.
Step 10: Calculate the value for the object result by calling the function operator + and operator -.
Step 11: Call the display function using obj1 and obj2 and result.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
cin>>a>>b;
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
void display()
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
Output:
Enter the value of Complex Numbers a, b
4 5
2 2
Input Values
4 + 5i
2 + 2i
Result
6 + 7i
2 + 3i
11. Define Pointer. Explain This pointer , Null pointer , Void pointer with examples.
Pointer is a variable whose value is the address of another variable. Like any variable or constant, you
must declare a pointer before you can work with it. The general form of a pointer variable declaration
is:
type *var-name;
Null Pointer:
It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have
exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned
NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries, including
iostream. Consider the following program:
#include <iostream>
using namespace std;
int main () {
int *ptr = NULL;
return 0;
}
When the above code is compiled and executed, it produces the following result:
This pointer:
The this pointer is passed as a hidden argument to all nonstatic member function calls and is available
as a local variable within the body of all nonstatic functions. this pointer is a constant pointer that
holds the memory address of the current object. this pointer is not available in static member functions
as static member functions can be called without any object (with class name).
Output:
x = 20
Void Pointer:
Void pointer or generic pointer is a special type of pointer that can be pointed at objects of any data
type. A void pointer is declared like a normal pointer, using the void keyword as the pointers type.
#include<stdio.h>
int main()
{
int a = 10;
void *ptr = &a;
printf("%d", *(int *)ptr);
return 0;
}
12. Define Binary operator overloading . Write a program for Binary operator
overloading.
13. Write a program for bubble sort program ( with pointers/ without pointers)
With ponters:
#include<stdio.h>
#include<stdlib.h>
int bubblesort(int *,int);
int bubblesort(int *arr,int n)
{
int i,j,temp;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-1-i);j++)
{
if(*(arr+j)>*(arr+(j+1)))
{
temp=*(arr+j);
*(arr+j)=*(arr+(j+1));
*(arr+(j+1))=temp;
}
}
return 0;
}
int main()
{
int *arr,i,n;
printf("Enter no. of terms\n");
scanf("%d",&n);
arr=(int *)malloc(n*sizeof(int));
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",(arr+i));
bubblesort(arr,n);
printf("Sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\n",*(arr+i));
return 0;
}
Without pointers:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, i, arr[50], j, temp;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort technique...\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!\n";
cout<<"Sorted list in ascending order :\n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
getch();
}
14. What is friend class / What is friend function? Write a program using friend
fuction.
A friend function of a class is defined outside that class' scope but it has the right to access all private
and protected members of the class. Even though the prototypes for friend functions appear in the
class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class template, in
which case the entire class and all of its members are friends.
Program:
//friend function
#include<iostream.h>
class cls1{
int a; //this a is for objects of class cls1
public:
void set_a(){
a=4; //setting a for objects of class cls1
}
friend void sum(cls1, cls2);
};
class cls2{
int a; //this a is for objects of class cls2
public:
void set_a(){
a=4; //setting a for objects of class cls2
}
friend void sum(cls1, cls2);
};
void main(){
cls1 ob1;
cls2 ob2;
Output: 8
Templates are the foundation of generic programming, which involves writing code in a way that is
independent of any particular type.
A template is a blueprint or formula for creating a generic class or a function. The library containers like
iterators and algorithms are examples of generic programming and have been developed using
template concept.
There is a single definition of each container, such as vector, but we can define many different kinds of
vectors for example, vector <int> or vector <string>.
Here, type is a placeholder name for a data type used by the function. This name can be used within
the function definition.
An exception is a problem that arises during the execution of a program. A C++ exception is a response
to an exceptional circumstance that arises while a program is running, such as an attempt to divide by
zero.
Exceptions provide a way to transfer control from one part of a program to another. C++ exception
handling is built upon three keywords: try, catch, and throw.
throw: A program throws an exception when a problem shows up. This is done using
a throw keyword.
catch: A program catches an exception with an exception handler at the place in a program
where you want to handle the problem. The catch keyword indicates the catching of an
exception.
try: A try block identifies a block of code for which particular exceptions will be activated. It's
followed by one or more catch blocks.
Assuming a block will raise an exception, a method catches an exception using a combination of
the try and catch keywords. A try/catch block is placed around the code that might generate an
exception. Code within a try/catch block is referred to as protected code, and the syntax for using
try/catch looks like the following:
try
{
// protected code
}catch( ExceptionName e1 )
{
// catch block
}catch( ExceptionName e2 )
{
// catch block
}catch( ExceptionName eN )
{
// catch block
}
There should be multiple catch statements to catch different type of exceptions in case your try block
raises more than one exception in different situations.