CS2203 OBJECT
ORIENTED PROGRAMMING
OOPS,CS2203 OBJECT ORIENTED PROGRAMMING QUESTION BANKS, anna university question bank for CSE, Anna University, QUESTION BANKS, VIVA-QUESTIONS,
TWO MARK QUESTION AND ANSWER
UNIT I
- Give some characteristics of
procedure-oriented language.
1.
Emphasis is on doing things (algorithms).
2. Larger programs are divided into smaller programs known as functions.
3. Most of the functions share global data.
4. Data move openly around the system from function to function.
5. Employs top-down approach in program design.
Function-1
Function-2
Function-3
Function-4
Function-5
Main program
- Write any four features of OOPS.
The features of OOPS are,
1. Emphasis is on data rather than on procedure.
2. Programs
are divided into objects.
3. Data is
hidden and cannot be accessed by external functions.
4. Follows
bottom -up approach in program design.
- What are the basic concepts of OOPS?
the
basic concepts of OOPS are,
1.
Objects.
2.
Classes.
3.
Data abstraction
and Encapsulation.
4.
Inheritance.
5.
Polymorphism.
6.
Dynamic binding.
7. Message
passing.
- What is object?
Objects are basic
run-time entities in an object-oriented system. They may represent a person, a
place, a bank account, a table of data or any item that the program has to
handle. Each object has the data and code to manipulate the data and theses
objects interact with each other.
- What is class?
1. Collection
of objects of similar type.
2. Once
a class has been defined, we can create any number of objects belonging to the
classes.
3.
Classes are user-defined data types
and behave like built-in types of the programming language.
- What is encapsulation?
- Wrapping up
of data and function within the structure is called as encapsulation.
- The
insulation of data from direct access by the program is called as data
hiding or information binding.
- The data is
not accessible to the outside world and only those functions, which are wrapped
in the class, can access it.
- What are data members and member
functions?
Classes use the concept of abstraction.
They are defined as a list of abstract attributes such as size, weight, and
cost and uses functions to operate on these attributes.
The attributes are sometimes called as data members
because they hold information. The functions that operate on these data are
called as member functions or methods.
Eg: int a, b; // a, b are data members
Void getdata ( ); // member function
- What is dynamic binding or late binding?
Dynamic binding means that the code
associated with a given procedure
call is not known until the time of
the call at the run-time.
- Write the process of programming in an
object-oriented language?
- Create
classes that define objects and their behavior.
- Creating
objects from class definition.
- Establishing
communication among objects.
- Give any four advantages of OOPS.
The advantages of OOPS
are,
1. The principle of data hiding helps the
programmer to build secure programs that cannot be invaded by code in other
parts of the program.
2. It is possible to have multiple instances of
an object to co-exist without any interference.
3. Object oriented programming can be easily
upgraded from small to large systems.
4.
Software complexity can be easily managed.
- What are the features required for
object-based programming Language?
The
features required for object-based programming are,
1. Data encapsulation.
2. Data hiding and access mechanisms.
3. Automatic initialization and clear up of
objects.
4.
Operator overloading.
- Give any four applications of OOPS.
The applications of OOPS are,
1. Real-time systems.
2. Simulation and modeling.
3. Object-oriented databases.
4.
AI and expert systems.
- What are tokens?
The
smallest individual units in a program are known as tokens. C++ has the
following tokens,
1. Keyword
2. Identifiers
3. Constants
4. Strings
5. Operator
- What are keywords?
The keywords implement specific C++
language features. They are explicitly reserved identifiers and cannot be used
as names fro the program variables or other user defined program elements.
Eg: go to, If, struct, else, union etc.
- Rules for naming the identifiers in C++.
1. Only alphabetic characters, digits
and underscore are permitted.
2. The name cannot start with a digit.
3. The upper case and lower case letters are distinct.
4. A declared keyword cannot be used as a variable name.
- What are the operators available in C++?
All operators in C are also used in
C++. In addition to insertion operator << and extraction operator
>> the other new operators in C++ are,
:: Scope resolution operator
:: * Pointer-to-member declarator
->* Pointer-to-member operator
.* Pointer-to-member operator
delete - Memory
release operator
endl - Line feed
operator
new - Memory
allocation operator
setw - Field width
operator
- What is a scope resolution operator?
Scope resolution operator is used to
uncover the hidden variables. It also allows access to global version of
variables.
Eg:
#include<iostream. h>
int m=10; // global variable m
void main ( )
{
int m=20; // local variable m
cout<<”m=”<<m<<”\n”;
cout<<”: : m=”<<:
: m<<”\n”;
}
output:
20
10 (: : m access global m)
Scope resolution operator is used to define the function
outside the class.
Syntax:
Return type <class name> : : <function name>
Eg:
Void x : : getdata()
- What is a default argument?
Default arguments assign a default
value to the parameter, which does not have matching argument in the function
call. Default values are specified when the function is declared.
Eg : float amount(float principle,int period, float
rate=0. 15)
Function call is
Value=amount(5000,7);
Here it takes principle=5000& period=7
And default value for rate=0.15
Value=amount(5000,7,0.34)
Passes an explicit value 0f 0.34 to rate
We must add default value from right to left
- What are constant arguments?
Keyword is const. The qualifier const
tells the compiler that the function should not modify the argument. The
compiler will generate an error when this condition is violated. This type of
declaration is significant only when we pass arguments by reference or pointers
eg: int strlen (const char *p);
- How the class is specified?
Generally class specification has two
parts
1. Class declaration
It describes the
type and scope of its member
2. Class function definition
It describes how
the class functions are implemented
The general form is
Class class_name
{
private:
variable declarations;
function declaration;
public:
variable declaration;
function declaration;
};
- How to create an object?
Once the class has been declared, we
can create variables of that type by using the class name
Eg: classname x; //memory for x is created
- How to access a class member?
We can access
the member function by using the following syntax,
object-name. function-name (actual
arguments);
eg:x.getdata(100,75.5);
- How the member functions are defined?
Member functions can be defined in two
ways
1. Outside the class definition
Member function can be defined by using scope resolution
operator ::
General format is
Return type class_ name:: function-name (argument declaration)
{
}
2. Inside the class definition
This method of defining member function is to replace the
function declaration by the actual function definition inside the class. It is
treated as inline function
Eg: class item
{
int a, b;
void getdata (int x, int y)
{
a=x;
b=y;
};
- What is static data member?
Static
variable are normally used to maintain values common to the entire class.
Feature:
1. It is initialized to zero when the first object is
created. No other
initialization is
permitted
2. only one
copy of that member is created for the entire class and is
shared by all the
objects
3. It is only
visible within the class, but its life time is the entire class type
and scope of each
static member variable must be defined outside the
class
4. It is stored
separately rather than objects
Eg: static
int count//count is initialized to zero when an object is created.
int
classname::count; //definition of static data member
- What is static member function?
A
member function that is declared as static has the following properties
1. A static function can have access to only other
static member declared
in the same class
2. A static member function can be called using the
classname as follows
class name :: function_name;
- How the objects are used as function
argument?
This can be done in two ways
1. A copy of the entire object is passed to the argument
2. Only address of the objects is transferred to the f
unction
- What is Friend function? Write the syntax.
A function that has access to the private
member of the class but is not itself a member of the class is called friend
functions.
The general form is
friend datatype function name (object dec);
Friend function is preceded by the keyword ‘friend’.
- Write some properties of friend functions.
1. Friend function is not in the scope of the class to
which it has been
declared as friend. Hence it cannot be
called using the object of that
class.
2. Usually it has object as arguments.
3. It can be declared either in the public or private part
of a class.
4. It cannot access member names directly. It has to use
an object name
and dot membership operator with each
member name. eg: ( A . x )
29. What is
function overloading? Give an example.
Function
overloading means we can use the same function name to create functions that
perform a variety of different tasks.
Eg: An overloaded add( ) function handles different data
types as shown below.
// Declarations
i. int add( int a, int b); //add function with 2 arguments
of same type
ii. int add( int a, int b, int c); //add function with 3
arguments of same type
iii. double add( int p, double q); //add function with 2
arguments of
different type
//Function calls
add (3 , 4); //uses prototype ( i. )
add (3, 4, 5); //uses
prototype ( ii. )
add (3 , 10.0); //uses prototype ( iii. )
- What is constant function?
When we define
the function as constant we should not modify the objects. If the functions are
modified to change the values, the error can be caught.
Syntax:
< Return type> <Function
name> ( ArgList ) const
{
Funtion body;
}
- What is constant object?
Const objects are
objects that are not modifiable. Only functions that are defined as Const can
be accessed by const objects. Even public variables of the object are not
modifiable.
Syntax:
const class_name obj_name;
- Define the nested class.
We have two classes. Outside class is
a nesting class and contains the entire body of the inside class. The inside
class, which is defined inside the outside class is called nested class.
32.
What is the syntax, if we defined the nested class as public?
Class outerClass
{
public:
Class innerClass
{
---------
---------
};
--------
--------
};
32.
What is the syntax, if we defined the nested class as private?
Class outerClass
{
Private:
Class innerClass;
Public:
---------
---------
};
Class outerClass :: innerClass
{
Definition
of innerClass;
-----------------
-----------------
}
33.
What is polymorphism? What are its types?
Polymorphism is the ability to take more than one form. An
operation may exhibit different behaviors in different. The behavior depends
upon the type of data used.
Polymorphism is of two types. They are
1. Function overloading
2. Operator overloading
UNIT – II
- Define 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 same
as class name. The constructor is invoked whenever an object of its associated
class is created. It is called constructor because it constructs the values of
data members of the class
Eg:
integer Class
{
……
public:
integer( );//constructo r
………
}
- Define default constructor.
The constructor with no arguments is called default
constructor.
Eg:
Class integer
{
int m,n;
Public:
Integer( );
…….
};
integer::integer( )//default constructor
{
m=0;n=0;
}
the statement
integer a;
invokes the default constructor
- Define parameterized constructor.
constructor with arguments is called parameterized
constructor.
Eg;
class integer
{
int m,n;
public:
integer(int
x,int y)
{
m=x;n=y;
}
To invoke parameterized constructor we
must pass the initial values as arguments to the constructor function when an
object is declared. This is done in two ways,
1.By calling the constructor explicitly
eg: integer int1=integer(10,10);
2.By calling the constructor implicitly
eg: Integer int1(10,10);
- Define default argument
constructor.
The constructor with default arguments are called default
argument constructor.
Eg:
Complex(float real,float imag=0);
The default value of the argument imag is 0
The statement complex a(6.0)
assign real=6.0 and imag=0
the statement
complex a(2.3,9.0)
assign real=2.3 and imag=9.0
- What is the ambiguity between default
constructor and default argument constructor?
The default
argument constructor can be called with either one argument or no arguments. When
called with no arguments, it becomes a default constructor. When both these
forms are used in a class, it cause ambiguity for a statement such as A a;
The ambiguity is whether to call A::A() or A::A(int i=0)
- Define copy constructor.
A copy
constructor is used to declare and initialize an object from another object. It
takes a reference to an object of the same class as an argument
Eg: integer i2(i1);
would define the object i2 at the same time initialize it
to the values of i1.
Another form of this statement is
Eg: integer i2=i1;
The process of initializing through a copy constructor is
known as copy initialization .
- Define destructor.
It is used to
destroy the objects that have been created by constructor. Destructor name is
same as class name preceded by tilde symbol(~)
Eg;
~integer()
{
}
A destructor never takes any arguments nor it does it
return any value. The compiler upon exit from the program will invoke it. new
Whenever operator is used to allocate memory in the constructor, we should
use delete to free that memory.
- Define multiple constructors
(constructor overloading).
The class that
has different types of constructor is called multiple constructors
Eg:
#include<iostream. h>
#include<conio.h>
class integer
{
int m,n;
public:
integer( ) //default constructor
{
m=0;n=0;
}
integer(int a,int b) //parameterized constructor
{
m=a; n=b;
}
integer(&i) //copy constructor
{
m=i. m;
n=i.n;
}
};
void main()
{
integer i1; //invokes default
constructor
integer i2(45,67);//invokes parameterized constructor
integer i3(i2); //invokes copy constructor
}
- Write some special characteristics of
constructor.
Special characteristics of constructor
are,
1. They should be declared in the public section
2. They are invoked automatically when the objects are
created
3. They do not have return types, not even void and
therefore, and they
cannot return values
4.They cannot be inherited, though a derived class can
call the base class
5. They can have default arguments
6. Constructors cannot be virtual f unction
- How the objects are initialized
dynamically?
To
call parameterized constructor we should the pass values to the object ie,for
the constructor integer(int a,int b) it is invoked by integer a(10,18)
this value can be get during run time. i.e., f or above
constructor
int p,q;
cin>>p>>q;
integer a(p,q);
- What
is operator overloading?
This mechanism of giving such special
meanings to an operator is known as Operator overloading. It provides a
flexible option for the creation of new definitions for C++ operators.
- List
out the operators that cannot be overloaded.
1. Class
member access operator (., .*)
2. Scope
resolution operator (::)
3. Size operator
(sizeof)
4. Conditional
operator (? :)
- List out the operators that cannot be
overload as friend functions.
a.
Assignment
operator =
b.
Function call
operator ()
c.
Array subscript
operator []
d.
Access to class
member using pointer to object operator ->
- What
is the purpose of using operator function? Write its syntax.
To define an
additional task to an operator, we must specify what it means in relation to
the class to which the operator is applied. This is done by Operator function ,
which describes the task. Operator functions are either member functions or
friend functions. The general form is
return type classname :: operator op(arglist )
{
function body
}
where return type is the type of value returned by
specified operation.
Op- operator being overloaded. The op is preceded by a
keyword operator. operator op is the function name.
- Write
at least four rules for Operator overloading.
Rules for Operator overloading are,
1. Only the
existing operators can be overloaded.
2. The overloaded
operator must have at least one operand that is of user
defined data type.
3. The basic
meaning of the operator should not be changed.
4. Overloaded
operators follow the syntax rules of the original operators.
5. They cannot be overridden.
- What
are the different types of conversion?
The different types of conversions are,
1. Basic type to
Class type.
2. Class type to Basic
type.
3. Class type to Class
type.
4. Basic type to Basic
type
- Write
the syntax to convert from class to basic type.
Operator
typename( )
{
…………
Function statements;
…………
}
- What
are the conditions to satisfy the type casting function?
1. it must be a
class member.
2. it must not
specify a return type.
3. it must not have
any arguments.
2. Larger programs are divided into smaller programs known as functions.
3. Most of the functions share global data.
4. Data move openly around the system from function to function.
5. Employs top-down approach in program design.
Function-4
Main program
The features of OOPS are,
2. Programs are divided into objects.
3. Data is hidden and cannot be accessed by external functions.
4. Follows bottom -up approach in program design.
the basic concepts of OOPS are,
- Wrapping up
of data and function within the structure is called as encapsulation.
- The
insulation of data from direct access by the program is called as data
hiding or information binding.
- The data is
not accessible to the outside world and only those functions, which are wrapped
in the class, can access it.
The attributes are sometimes called as data members because they hold information. The functions that operate on these data are called as member functions or methods.
Eg: int a, b; // a, b are data members
Void getdata ( ); // member function
- Create
classes that define objects and their behavior.
- Creating
objects from class definition.
- Establishing
communication among objects.
The advantages of OOPS are,
The features required for object-based programming are,
The applications of OOPS are,
The smallest individual units in a program are known as tokens. C++ has the following tokens,
Eg: go to, If, struct, else, union etc.
2. The name cannot start with a digit.
3. The upper case and lower case letters are distinct.
4. A declared keyword cannot be used as a variable name.
:: * Pointer-to-member declarator
->* Pointer-to-member operator
.* Pointer-to-member operator
delete - Memory release operator
endl - Line feed operator
new - Memory allocation operator
setw - Field width operator
#include<iostream. h>
int m=10; // global variable m
void main ( )
{
int m=20; // local variable m
cout<<”m=”<<m<<”\n”;
cout<<”: : m=”<<: : m<<”\n”;
}
output:
20
10 (: : m access global m)
Scope resolution operator is used to define the function outside the class.
Syntax:
Return type <class name> : : <function name>
Eg:
Void x : : getdata()
Eg : float amount(float principle,int period, float rate=0. 15)
Function call is
Value=amount(5000,7);
Here it takes principle=5000& period=7
And default value for rate=0.15
Value=amount(5000,7,0.34)
Passes an explicit value 0f 0.34 to rate
We must add default value from right to left
eg: int strlen (const char *p);
1. Class declaration
It describes the type and scope of its member
2. Class function definition
It describes how the class functions are implemented
The general form is
Class class_name
{
private:
variable declarations;
function declaration;
public:
variable declaration;
function declaration;
};
Eg: classname x; //memory for x is created
We can access the member function by using the following syntax,
eg:x.getdata(100,75.5);
Member function can be defined by using scope resolution operator ::
General format is
Return type class_ name:: function-name (argument declaration)
{
}
2. Inside the class definition
This method of defining member function is to replace the function declaration by the actual function definition inside the class. It is treated as inline function
Eg: class item
{
int a, b;
void getdata (int x, int y)
{
a=x;
b=y;
};
Static variable are normally used to maintain values common to the entire class.
Feature:
1. It is initialized to zero when the first object is created. No other
2. only one copy of that member is created for the entire class and is
3. It is only visible within the class, but its life time is the entire class type
4. It is stored separately rather than objects
Eg: static int count//count is initialized to zero when an object is created.
int classname::count; //definition of static data member
A member function that is declared as static has the following properties
1. A static function can have access to only other static member declared
2. A static member function can be called using the classname as follows
class name :: function_name;
1. A copy of the entire object is passed to the argument
2. Only address of the objects is transferred to the f unction
The general form is
friend datatype function name (object dec);
Friend function is preceded by the keyword ‘friend’.
1. Friend function is not in the scope of the class to which it has been
2. Usually it has object as arguments.
3. It can be declared either in the public or private part of a class.
4. It cannot access member names directly. It has to use an object name
Eg: An overloaded add( ) function handles different data types as shown below.
// Declarations
i. int add( int a, int b); //add function with 2 arguments of same type
ii. int add( int a, int b, int c); //add function with 3 arguments of same type
iii. double add( int p, double q); //add function with 2 arguments of
different type
//Function calls
add (3 , 4); //uses prototype ( i. )
add (3 , 10.0); //uses prototype ( iii. )
Polymorphism is the ability to take more than one form. An operation may exhibit different behaviors in different. The behavior depends upon the type of data used.
Polymorphism is of two types. They are
1. Function overloading
2. Operator overloading
A constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class
Eg:
integer Class
{
……
public:
integer( );//constructo r
………
}
The constructor with no arguments is called default constructor.
Eg:
Class integer
{
int m,n;
Public:
Integer( );
…….
};
integer::integer( )//default constructor
{
m=0;n=0;
}
the statement
integer a;
invokes the default constructor
constructor with arguments is called parameterized constructor.
Eg;
class integer
{
public:
integer(int x,int y)
{
}
1.By calling the constructor explicitly
eg: integer int1=integer(10,10);
2.By calling the constructor implicitly
eg: Integer int1(10,10);
The constructor with default arguments are called default argument constructor.
Eg:
Complex(float real,float imag=0);
The default value of the argument imag is 0
The statement complex a(6.0)
assign real=6.0 and imag=0
the statement
complex a(2.3,9.0)
assign real=2.3 and imag=9.0
The ambiguity is whether to call A::A() or A::A(int i=0)
Eg: integer i2(i1);
would define the object i2 at the same time initialize it to the values of i1.
Another form of this statement is
Eg: integer i2=i1;
The process of initializing through a copy constructor is known as copy initialization .
Eg;
~integer()
{
}
A destructor never takes any arguments nor it does it return any value. The compiler upon exit from the program will invoke it. new Whenever operator is used to allocate memory in the constructor, we should
use delete to free that memory.
Eg:
#include<conio.h>
{
public:
integer( ) //default constructor
{
}
integer(int a,int b) //parameterized constructor
{
}
integer(&i) //copy constructor
{
}
void main()
{
integer i2(45,67);//invokes parameterized constructor
integer i3(i2); //invokes copy constructor
1. They should be declared in the public section
2. They are invoked automatically when the objects are created
3. They do not have return types, not even void and therefore, and they
4.They cannot be inherited, though a derived class can call the base class
5. They can have default arguments
6. Constructors cannot be virtual f unction
To call parameterized constructor we should the pass values to the object ie,for the constructor integer(int a,int b) it is invoked by integer a(10,18)
this value can be get during run time. i.e., f or above constructor
int p,q;
cin>>p>>q;
integer a(p,q);
1. Class member access operator (., .*)
2. Scope resolution operator (::)
3. Size operator (sizeof)
4. Conditional operator (? :)
return type classname :: operator op(arglist )
{
function body
}
where return type is the type of value returned by specified operation.
Op- operator being overloaded. The op is preceded by a keyword operator. operator op is the function name.
1. Only the existing operators can be overloaded.
2. The overloaded operator must have at least one operand that is of user
3. The basic meaning of the operator should not be changed.
4. Overloaded operators follow the syntax rules of the original operators.
0 comments:
Post a Comment