0% found this document useful (0 votes)
85 views19 pages

OOP Material

The document compares and contrasts procedures oriented programming (POP) and object oriented programming (OOP). In POP, programs are divided into functions and data is shared globally, while in OOP programs are divided into objects and data is private and encapsulated. OOP follows a bottom-up approach and allows for features like classes, inheritance, and polymorphism, while POP follows a top-down approach and lacks these features. Common programming languages for each paradigm are also listed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
85 views19 pages

OOP Material

The document compares and contrasts procedures oriented programming (POP) and object oriented programming (OOP). In POP, programs are divided into functions and data is shared globally, while in OOP programs are divided into objects and data is private and encapsulated. OOP follows a bottom-up approach and allows for features like classes, inheritance, and polymorphism, while POP follows a top-down approach and lacks these features. Common programming languages for each paradigm are also listed.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

1

Q:-1 Difference between Procedures Oriented Programming (POP) and Object


Oriented Programming.(OOP)

POP(C) OOP(C++)

In POP, importance is given to the In OOP, importance is given to the data.


sequence of things to be done i.e.
algorithms

In POP, larger programs are divided into In OOP, larger programs are divided into
functions objects.

In POP, most functions share global data In OOP mostly the data is private and only
i.e data move freely around the system functions inside the object can access the
from function to function. data.

POP follows a top down approach in OOP follows a bottom up approach.


problem solving

In POP, adding of data and function is In OOP it is easy.


difficult.

In POP, there are no access specifiers. In OOP there are public, private and
protected specifiers.

In POP, operator cannot be overloaded In OOP operator can be overloaded.

A function transforms data from one from Objects may communicate with each other
to another. through functions

Data is not hidden easily to access. Data is hidden and can’t access by other
function

POP has no facility like class, abstraction, OOP has facility like class, abstraction,
inheritance, encapsulation, inheritance, encapsulation,
polymorphism..etc polymorphism..etc

Eg ..C, QBASIC, PASCAL are procedure Eg.. C++, JAVA are Object Oriented
oriented language. Language.

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
2

Q:-2 Explain Characteristics of OOP.


1. Emphasis is on data rather than procedure.
2. Programs are divided into what are known as objects.
3. Data and functions are tied together in the data structure.
4. Data is hidden and can not be accessed by external functions.
5. Objects may communicate with each other through functions.
6. New data and functions can be easily added whenever necessary.
7. Bottom-up approach.

Q:-3 what is Class, Objects? Explain with example.


Class:- Objects are variable of class type. Once a class is defined, we can create any
number of objects belonging to that class. Classes are user-defined data types and behave
like built-in types of programming languages.

Objects: - Objects are basic run-time entities. They contain data and code together. They
may represent a person, a place, a bank account, a table of data, or any item in the
program. Program objects should be chosen such that they match closely with the real
world. Objects take up space in memory. Objects can communicate with one another by
sending messages.
Example:-
#include <iostream.h>
#include <conio.h>
class test //Class name
{
int data;
public:
void read() //method
{
cout<<"Enter any no:=>";
cin>>data;
}
void show() //method
{
cout<<data<<endl;
}
};
void main()
{
clrscr();
test t; //define object for accessing a class.
t.read();
t.show(); getch();}
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
3

Q:-4 what is visibility modifier OR access specifiers? List out differentiate with
proper example
To provide the facility of security by means of data hiding, is get done by some access
specifies and these access specifies are known as visibility modifiers or access
specifiers.
These visibility modifiers are used to secure the data member and member function of
a class.
There are threes types of visibility modifiers 1. Public 2. Private 3. Protected
Public variables are variables that are visible to all classes.
Private variables are variables that are visible only to the class to which they belong.
Protected variables are variables that are visible only to the class to which they belong,
and any subclasses
Deciding when to use private, protected or public variable is sometimes tricky. You
need to think whether or not an external object (or program), actually needs direct access
to the information. If you do want other objects to access internal data, but wish to
control it, you would make it either private or protected, but provide functions which can
manipulate the data in a controlled way.

Q:-5 what are the different between class and structure?


Class Structure
A class is way to bind the data and its A structure is a user defined data type
associated functions together. And it allows which is the collection of different of
the data to be hidden from external use. elements who having different Data types.
A class has three access modifiers. A structure has not any access modifier.
Public, Private, Protected
Class keyword is used to declare the class Struct keyword is used declare the
structure.
Class binds both data and member Structure contain only data.
functions.
Syntax: Syntax:
Class <class name> Struct <structure name>
{ {
Private: data type variable1;
Public: data type variable2;
Protected: data type variable2;
}; };
Q:-6 what is encapsulation or what is data hiding? How it is implemented in C++?
The wrapping up of data and function into a single unit (called class) is known as
encapsulation. Data encapsulation is the most striking feature of a class. The data is not
accessible to the outside world, and only those functions which are wrapped in the class
can access it.

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
4

In general, encapsulation is the inclusion of one thing within another thing so that the
included thing is not apparent. Decapsulation is the removal or the making apparent a
thing previously encapsulated.
In OOP, encapsulation is the inclusion within a program object of all resources need
for the object to function – basically, the methods and the data. The object is said to
publish its interfaces. Other objects add here to these interfaces to use the object without
having to be concerned with how the object accomplishes it.
An object can be through of as a self –contained atom. The object interface consists of
public methods and instantiated data.
Example
#include <iostream.h>
#include <conio.h>
class Square
{
private:
int Num;

public:
void Get()
{
cout<<"Enter Number:";
cin>>Num;
}
void Display()
{
cout<<"Square Is:"<<Num*Num;
}
};

void main()
{
Square Obj;
Obj.Get();
Obj.Display();
getch();
}
Q:-7 Explain scope resolution operator with example.
In C, the global version of a variable can not be accesses from within the inner block.
C++ resolves this problem by introducing a new operator :: called the scope resolution
operator. This can be used uncover a hidden variable.

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
5

Syntax ::variable name


Example:-
#include <iostream.h>
#include <conio.h>
int m= 10;//global m
main()
{
int m=20; //m redeclared, local to main
clrscr();
{
int k=m;
int m=30; //m declared again local to inner block
cout<<"we are inner block \n";
cout<<"k= "<< k << "\n";
cout<<"m= "<< m << "\n";
cout<<"::m=" <<::m << "\n";
}
cout<<"we are outer block \n";
cout<<"m= "<< m << "\n";
cout<<"::m=" <<::m << "\n";
}
Note:- It is to be noted ::m will always refer to the global m

Q:-8 what is constructor? List out the characteristics of it. Explain with example.
A constructor is a ‘special’ member function whose task is to initialize the objects of
its class. It is special because its name is the same as the class name. constructor is
involved is declared and defined as follow:
Example:
class integer
{
int m,n;
public:
integer()//declaring the constructor
{
m=25;n=96;
cout<<m<<endl<<n;
}
};
main()
{
clrscr();
integer i1;
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
6

getch();
}
Characteristics:-
They should be declared in the public section.
They are invoked automatically when the objects are created.
They do not have return types.
They can not be inherited, though a derived class can call the base constructor.
They have default arguments.
Constructor can not be virtual.
We can not refer to their address.

Q:-9what is parameterized constructor? How constructor called


explicitly/implicitly?
C++ permits us to achieve these objectives by passing arguments to the constructor
function when the objects are created. The constructors that can take arguments are called
parameterized constructor.
We must pass the initial values as arguments to the constructor function when an
object is declared. This can be done in two ways:
1. By calling the constructor explicitly.
2. By calling the constructor implicitly.
Example
#include <iostream.h>
#include <conio.h>
class para_cons
{
int m,n;
public:
para_cons(int,int); //constructor declared
void display()
{
cout<<"m ="<< m << "\n";
cout<<"n ="<< n << "\n";
}
};
para_cons::para_cons(int x, int y) //constructor defined
{
m=x; n=y;
}
main()
{
clrscr();
para_cons obj1(10,20); //constructor called implicitly
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
7

para_cons obj2=para_cons(100,200); //constructor called explicitly


cout << "\n OBJECT 1" << "\n";
obj1.display();
cout << "\n OBJECT 2" << "\n";
obj2.display();
getch();
}

Q:-10Explain constructor with default argument?


It is possible to define constructor with default arguments. In following example
complex (float r, float i=0) The default value of the argument i is Zero. Then , the
statement complex obj1(10.51) assigns the value 10.51 to r variable and 0.0 to i
Example
class complex
{
float real,imag;
public:
complex(float r,float i=0)//constructor declared
{
real=r;
imag=i;

}
void display()
{

cout<<"Real=>"<< real << "\n";


cout<<"Imag=>"<< imag << "\n";
}
};
void main()
{
clrscr();
complex obj1(10.51); //constructor called implicitly
cout << "\n OBJECT 1" << "\n";
obj1.display();
getch();
}
Q:-11what is constructor? When copy constructor is called? What is an advantage
of using copy constructor?
A constructor is a ‘special’ member function whose task is to initialize the objects of
its class. It is special because its name is the same as the class name.
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
8

Copy constructor is used to declare and initialize an object from another object.For
example, the statement code B (A);
Would define the object B and at the same time initialize it to the value of A.
Another from of statement is code B = A;
The process of initializing through a copy constructor is known as copy initialization.
Remember the statement B=A; will not invoke the copy constructor. However, if B
and A are objects, this statement is legal and simply assign the values A to B, member by
member. This is the task of the overloaded assignment operator (=).
A copy constructor takes a reference to an object of the same class as itself as an
argument. Let us consider a simple example of constructing and using a copy constructor
as shown in program.
Example
class copy_cons
{
int id;
public:
copy_cons(){};//constructor declared
copy_cons(int a) //constructor again
{
id=a;
}
copy_cons(copy_cons & x)//copy constructor
{
id=x.id;
}
void display()
{
cout<<id;
}
};
void main()
{
clrscr();
copy_cons A(100); //Object A is created and initialize
copy_cons B(A); //Copy constructor called
copy_cons C=A;//Copy constructor called again
copy_cons D; //D is created, not initialized
D=A; //Copy constructor not called
cout <<"\n id of A:=>";A.display();
cout <<"\n id of B:=>";B.display();
cout <<"\n id of C:=>";C.display();
cout <<"\n id of D:=>";D.display();
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
9

getch();
}
Advantages
A reference variable has been use an argument of the copy constructor. We can not
pass the argument by value to a copy constructor.
When no copy constructor is defined the compiler supplies its own copy constructor.
Q:-12what do you mean by constructor in derived class? If constructor function in
derived and base classes, then which constructor function get executed explain with
example.
The constructor play important role in initializing an object’s data members and
allocating required resources such a memory.
The derived class need not have a constructor as long as the base class has a no
argument constructor.
However if the base class has the constructor with argument then it is mandatory for
the derived class to have constructor and pass the argument to the base class constructor.
When an object of a derived class is created, the constructor of a base class is executed
first and later the constructor of derived class.
Example
class base //base class
{
public:
base()//constructor declared
{
cout<<" base class executed \n";
}
};
class derived:public base
{
public:
derived()
{
cout<<"derived class executed\n";
}
};
void main()
{
clrscr();
derived d; //access both base constructors
getch();
}

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
10

Q:-13what is destructor? How it is written in C++? When is called? Also list out
special properties of destructor. Can we write virtual destructor?
A destructor, as the name implies, is used to destroy the objects that have been created
by a constructor. Like constructor, the destructor is member function whose name is the
same as the class name but is preceded by tilde (~).
A destructor never takes any argument nor does return any value.
The destructor has no return type like the constructor, since it is invoked automatically
whenever an object goes out of scope.
It is a good practice to declare destructors in a program since it releases memory space
for future use.
Syntax:-
~classname ( )
Example
int count=0;
class dest //base class
{
public:
dest()//constructor declared
{
count++;
cout<<"\n No. of object created "<< count;
}
~dest()//distructor declared
{
cout<<"\n No. of object created "<< count;
count--;
}
};
void main()
{
clrscr();
cout << "\n\n Enter Main\n";
dest a1,a2,a3,a4; //objects creation
cout << "\n\n Re-enter main\n";
getch();
}

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
11

Q:-14 Explain memory management operator.Disscuss advantages of NEW over


MALLOC
There are two types of memory management operators in C++:
• new
• delete
These two memory management operators are used for allocating and freeing memory
blocks in efficient and convenient ways.

New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be
used to create object of any type.

General syntax of new operator in C++:


The general syntax of new operator in C++ is as follows:
pointer variable = new datatype;
In the above statement, new is a keyword and the pointer variable is a variable of type
datatype.

For example:
int *a=new int;

In the above example, the new operator allocates sufficient memory to hold the object
of datatype int and returns a pointer to its starting point. The pointer variable a holds the
address of memory space allocated.
Dynamic variables are never initialized by the compiler. Therefore, the programmer
should make it a practice to first assign them a value.

delete operator:
The delete operator in C++ is used for releasing memory space when the object is no
longer needed. Once a new operator is used, it is efficient to use the corresponding delete
operator for release of memory.

General syntax of delete operator in C++:


The general syntax of delete operator in C++ is as follows:
delete pointer variable;

In the above example, delete is a keyword and the pointer variable is the pointer that
points to the objects already created in the new operator. Some of the important points the
programmer must note while using memory management operators are described below:
• The programmer must take care not to free or delete a pointer variable that has
already been deleted.
• Overloading of new and delete operator is possible
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
12

• We know that sizeof operator is used for computing the size of the object. Using
memory management operator, the size of the object is automatically computed.
• The programmer must take care not to free or delete pointer variables that have
not been allocated using a new operator.

Example
void main()
{
//Allocates using new operator memory space in memory for storing a integer datatype
clrscr();
int *a= new int;
*a=100;
cout << " The Output is:a= " << *a <<endl;
//Memory Released using delete operator
delete a;
getch();
}

Advantages of NEW over MALLOC


1. It automatically computes the size data object. We need not use the
sizeof ( ) operator.
2. It automatically returns the correct pointer type, so that there is no need to use a
type cast.
3. It is possible to initialize the object while creating the memory space.
4. Like any other operator new and delete can be overloaded.
Q:-15 Write major differences between constructor and destructor.
Constructor Destructor
1. Its name is same as class name. 1. Its name is same as class name preceded
by ~ (tilde).
2. It is called automatically when an object 2. It is called automatically when an object
is created. is destroyed.
3. It can have any number of arguments. 3. It never takes any argument.
4. Normally new is used in constructor to 4. Normally ‘delete’ is used in destructor to
allocate memory to objects. free memory from objects.

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
13

Q:-16 Explain Inline function. In which circumstances function can make inline?
Explain with example. Discuss advantages and disadvantages of it.
To eliminate the cost of calls to small function, C++ proposes new features called
inline function.
One of the objectives of using functions in a program is to save some memory space,
which becomes appreciable when a function is likely to be called many times.
However, every time a function is called, it takes a lot of extra time in executing a
series of instructions for tasks such as jumping to the function, saving register, pushing
argument into the stack, returning to the calling function.
When a function is small, substantial percentage of execution time may be spent in
such overhead.
C++ has a different solution to this problem. It is inline function.
An inline function is function that is expanded in line when it is invoked. That is, the
compiler replaces the function call with the corresponding function code
Syntax:-
inline function-header
{
Function body
}
It is easy to make a function inline. All we need to do is to prefix the keyword inline to
the function definition. All inline functions must be defined before they are called.
We should exercise care before making a function inline. The speed benefits of line
function diminish (reduce) as the function grows in size.
At some point the overhead of the function call becomes small compared to the
execution of the function, and the benefits of inline function may be lost.
In such cases, the use of normal functions will be more meaningful.
Example:-
inline float mul(float p,float q)
{
return p*q;
}
inline float div(float x,float y)
{
return x/y;
}
void main()
{
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
14

clrscr();
float a,b,ans,ans1;
cout<<"enter the value of a = ";
cin>>a;
cout<<"enter the value of b = ";
cin>>b;
ans=mul(a,b);
ans1=div(a,b);
cout<<"multiplication is = "<<ans<<endl;
cout<<"division is = "<<ans1;
getch();
}
With the help of prefix inline we can easily make the inline function.
Some of the situations where inline expansion may not work are:
1. For functions returning values, if a loop, a switch, or to a goto exists.
2. For function not returning values, if a return statement exists.
3. If function contain static.
4. If inline function are recursive
Advantages
Unlike normal function call, inline function improves the speed of program execution.
You should use inline functions when the number of statements is small and when the
function is called infrequently.
An inline function is a small function with no overhead.
Disadvantages
The code is expanded during compilation so that executable file is large.
Some compilers may not support them correctly.

Q:-17what do you mean by object as function arguments? Explain call by value and
call by reference.
An object may be used as a function argument. This can be done in 2 ways:
1. A copy of the entire object is passed to the function
2. Only the address of the object is transferred to the function.
The first method is called pass by value. Since copy of the object inside the function do
not affect the object used to call the function.
The second method is called pass by reference. When an address of the object is passed
the called function works directly on the actual object used in the call.

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
15

Call by value Call by references


1) The process of passing the actual value 1) The process of calling a function using
of variable is known as call by value. pointers to pass the address of variable is
known as call by reference.
2) It has considered value of variable. 2) It has considered address of variable.
3) In this, it will not use any particular sign. 3) In this it will use a sign “*” and “&”
4) It will not use in all type of function. 4) It will use in all type of function.
5) In language C, most of programs are 5) In language C, we have to specify than it
execute through call by value. will execute through call by reference.
6)Example 6)Example
int add(int a, int b) swap(int * x, int * y)
{ {
int ans; int z;
ans=a+b; z= *x;
return ans; x= *y;
} *y=z;
}

Q:-18 what is static keyword? Explain static data members & static member
function. How static member’s functions are called?
Declaring class members or methods as static, makes them callable from outside the
object context. A member or method declared with static can not be accessed with
variable that is an instance of the object and can not be redefined in an extending class
Static variables are normally used to maintain values common to the entire class.
Static data member
A data member of a class can be qualified as static. The properties of a static member
variable are similar to that of a C static variable.
Characteristics
It is initialized to zero when first object of its class is created.
Only one copy of that member is created for the entire class and is shared by all the
objects of that class, no matter how many objects are created.
It is visible only within the class, but its lifetime is the entire program.
Static member function
A static function can have access to only other static members declared in the same
class
A static member function can be called using the class name

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
16

Example
class test
{
int code;
static int count;//static member variable
public:
void setcode()
{
code=++count;
}
void showcode()
{
cout<<"Object number:=>"<<code<<"\n";
}
static void showcount()//static member function
{
cout<<"Count:=>"<<count<<"\n";
}
};
int test::count;
void main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount(); //accessing static function
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
17

Q:-19what is friend function? Why we need to write friend function? Explain with
example. Discuss its advantages.
We know that the private members can not be accessed from outside the class. That is,
a non member function cannot have an access to the private data of a class.
For example, consider a case where two classes, manager and scientist, have been
defined. We would like to use a function income_tax ( ) to operate on the object of both
classes. In the situations, C++ allows the common function to be made friendly with both
classes, there by allowing the function to have access to the private data of these classes.
Syntax
Class Abc
{
Public:
………….
………….
friend void xyz(void); //declaration
};
The function declaration should be preceded by the keyword friend.
Characteristics of friend function
It is not in the scope of the class to which it has been declared as friend.
Since it is not in the scope of the class, it can not be called using the object of that
class.
It can be invoked like a normal function without the help of any object.
Unlike member function, it can not access the member names directly and has to use
an object name and not membership operator with each member name(eg. A.x).
It can be declared either in the public or the private part of a class without affecting its
meaning.
Usually, it has the objects as arguments.
The friend functions are often used in operator overloading.
Example
#include <iostream.h>
#include <conio.h>
class sample
{
int a;
int b;
public:
void setvalue()
Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.
SDJ INTERNATIONAL COLLEGE
18

{
a=25;b=40;
}
friend float mean(sample s);
};
float mean (sample s)
{
return float(s.a + s.b)/2.0;
}
void main()
{
clrscr();
sample x;
x.setvalue();
cout << "Mean Value:=> " << mean(x)<<"\n";
getch();
}
Q:-20Explain default argument and function prototyping.
Default arguments
C++ allows us to call a function without specifying all its arguments. In such cases, the
function assigns default value to the parameter which does not have matching argument
in the function call. Default values are specified when the function is declared.
Example
Class interest
{
Public:
float amt(flaot p, int n, flaot r=0.15)
{
return p*r*n;

}};
void main()
{
interest p;
cout << p.amt(1000,5,0.12) <<endl;
cout << p.amt(1000,5) <<endl;
}

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE
19

Function prototyping
The prototype describes the function interface to the compiler by giving details such as
the number and type of arguments and the type of return values.
With function prototyping, a template is always use when declaring and defining a
function.
When function is called, the compiler users the template to ensure that proper
arguments are passed, and the return value is treated correctly.
Any violation is matching the arguments or the return types will be caught by the
compiler at the time of compilation itself.
Function prototyping is declaration statement in the calling program and is of the
following form:
type function-name(argument-list) //syntax
int volume(int x, int y,int z ) //example
Note that each argument variable must be declared independently inside the
parentheses. That is combine declaration like int volume (int x, int y, z) //is illegal
example
The declaration int volume (int, int, int ) is acceptable at the place of declaration. At
this stage, the compiler only checks for the type of arguments when the function is called.

Prepared by: - Dr.Vimal Vaiwala Msc (I.T) ,PHD.


SDJ INTERNATIONAL COLLEGE

You might also like