Você está na página 1de 15

OOP using C++

July 2012(july)
1. Explain Any ten
i. Static variables: Static variables are those variables that are used to maintain values
common to the entire class. It is initialized to zero when the first object of its class is
created. It is visible only within the class, but its lifetime is the entire program.
ii. Global Variable : Global Variables declared outside of a block are called global variables.
Global variables have program scope, which means they can be accessed everywhere in
the program, and they are only destroyed when the program ends.
iii. Arrays in C++: An array is a collection of items of same data type. It is a derived data type.
iv. Iostreams : see notes
v. Inline member function : see notes
vi. Keywords : see notes
vii. Identifiers: see notes
viii. Header files: see notes
ix. Cout : see notes
x. Cin: see notes
xi. Basic data types : see notes
xii. Symbolic constants :
xiii. Compiler:
xiv. Software: see notes
xv. Pointers in C++. see notes
2. Explain any ten(10) of the following terms.
i. Class : A class is a collection of objects of similar type. Classes are user-defined data types
and behave like the built-in types of a programming language. The syntax used to create
an object is no different than the syntax used to create an integer object in C. If fruit has
been defined as a class, than the statement
fruit mango;
will create an object belonging to the class fruit.
ii. Static binding: Polymorphism is one of the crucial features of OOP. It simply means one
name, multiple forms. Polymorphism is implemented using the overloaded functions and
operators. The overloaded member functions are selected for invoking by matching
arguments both type and number. This information is known to the compiler at the time
of the compile time and therefore, compiler is able to select the appropriate function for a
particular call at the compile time itself. This is called early binding or static binding or
static linking. Also known as compile time polymorphism, early binding simply means that
an object is bound to its function call at compile time.
iii. Dynamic binding: If the function is linked with a particular class much later after the
compilation, this process is termed as late binding. It is also known as dynamic binding
because the selection of the appropriate function is done dynamically at run time.
Dynamic binding is one of the powerful features of C++. This requires the use of pointers
to objects. Object pointers and virtual functions are used to implement dynamic binding.
iv. Base class : The mechanism of deriving a new class from an old one is called inheritance.
The old class is referred to as base class. When a base class is publicly inherited by a
derived class, public members of the base class become public members of the derived
class. When a base class is privately inherited by a derived class, public members of the
base class become private members of the derived class
v. Child class : same as derived class
vi. Public access specifiers: see notes
vii. Void main(){ }: It is a function definition. The name of the function is main() and return
data type is void.
viii. Int main(){}: It is a function definition. The name of the function is main() and return data
type is int. This function should return an integer.
ix. Virtual function : The appropriate member function can be selected while the program is
running. This is known as run-time polymorphism. C++ supports a mechanism known as
virtual function to achieve run time polymorphism.
x. Function overloading: Using a single function name to perform different types of tasks is
known as function overloading.
xi. Operator overloading : Overloading means assigning different meanings to an operation
depending on the context. C++ permits overloading of operators, thus allowing us to
assign multiple meanings to operators. The << and >> operators are good examples of
operator overloading. Almost all the C++ operators can be overloaded with a few
exceptions such as member access operators ( . and .*), conditional operators ( ? :) and
the size operator (sizeof).

xii. Data hiding: 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. These functions provide the interface between the objects data and the
program. This insulation of the data from direct access by the program is called data
hiding or information hiding.
xiii. Polymorphism : Polymorphism, a Greek term, means the ability to take more than one
form. An operation may exhibit different behaviors in different instances. The behavior
depends upon the type of data used in the operation. The process of making an operator
to exhibit different behaviors in different instances is known as operator overloading.
Using a single function name to perform different types of tasks is known as function
overloading. Polymorphism plays an important role in allowing objects having different
internal structure to share the same external interface.

3. Differentiate between the following terms
i. Structure and class
Structure Class
i. It is a collection of elements
ii. By default, all members are public.
iii. In general, it declares only data
i. It is collection of objects.
ii. By default, all members are private.
iii. It declares class member functions
member. with the data member.
iv. It divides its member into parts
through access specifiers: private,
public, protected.

ii. Multiple and multilevel inheritance
Multiple inheritance Multilevel inheritance
i. Multiple inheritance refers to a class
being derived from two or more classes
ii. Multiple inheritance is supported by
C++, but not by Java and C#. Java and
C# recommend that instead of multiple
inheritance the design should be
inheriting from one class and
implementing other interfaces.
i. Multilevel inheritance refers to a class
inheriting from a parent class which is
itself derived from another class.
ii. Multilevel inheritance is supported by
all OOPs languages

iii. Private and protected access specifiers
iv. Constructor and destructor
Constructor Destructor
i. Constructor functions are invoked
automatically when objects are created.
ii. Constructor can have arguments.
iii. We can refer to their addresses.
i. They are invoked automatically when
objects are destroyed.
ii. No arguments can be provided to a
destructor.
iii. It is not possible to take the address of
a destructor.

v. Virtual function and pure virtual function
Virtual functions must be implemented in the base class, but they need not be
implemented in their derived classes. Derived classes will automatically inherit the base
class implementations of all virtual functions, but can override those functions by
providing their own implementations. The base class methods can still be called explicitly
to provide common functionality. A function that is declared virtual is expected to be
implemented by derived classes, but it is not a requirement.

Pure-virtual functions need not be implemented in the base class, but they must be
implemented in the derived classes. Any class that declares a pure-virtual function is
automatically rendered an ADT (abstract data type) and cannot be instantiated other than
through derivation. Derived classes do not inherit any of the abstract base class
implementations, but they can call them explicitly. Derived classes that do not implement
all the pure-virtual functions inherited from their abstract base classes are automatically
rendered abstract themselves, however any implementations they do provide are
subsequently treated as virtual methods (and are therefore inherited by derivatives). A
concrete class is a class that inherits or implements the sum total of all pure-virtual
methods inherited from all abstract base classes. Only concrete classes can be instantiated
without being derived from.
vi. Actual arguments and formal arguments
Actual arguments:
The arguments that are passed in a function call are called actual arguments. These
arguments are defined in the calling function.
Formal arguments:
The formal arguments are the parameters/arguments in a function declaration. The scope
of formal arguments is local to the function definition in which they are used. Formal
arguments belong to the called function. Formal arguments are a copy of the actual
arguments. A change in formal arguments would not be reflected in the actual arguments.

vii. Member function and friend function:
Member function Friend function
i. Member functions are invoked with the
help of an object.
ii. A member function is in the scope of
the class to which it has been declared
as a friend.
iii. A member function can access the
member names directly.

i. A friend function can be invoked like a
normal function without the help of an
object
ii. A friend function is not in the scope of
the class to which it has been declared
as a friend.
iii. A friend function cannot access the
member names directly.
iv. It has objects as arguments.

viii. Virtual base class and abstract base class:
Virtual base class can be overridden by any class. Virtual base classes are used only when
an object inherits the base more than once. Their use causes only one copy of the base
class to be present in the object. The implementation of a virtual member can be changed
by derived class.
Abstract class is one that is designed only to act as a base class( to be inherited by other
class). An abstract class is not used to create objects. An abstract class cannot instantiate,
It is always either partially implement or not implement. A class that inherit from abstract
class would have to implement the abstract method.
ix. Super base class and indirect base class
4. Answer any five
i. What is an object? Explain how an object is created from a class with an example. List all
the concepts of OOP. (1 +2 +2 =5)
Objects are the basic run-time entities in an OOP. They may represent a person, place, a
bank account, a table of data or any item that the program has to handle.
The syntax used to create an object is no different than the syntax used to create an
integer variable. Objects of a class can be created using the class tag name as type
specifier.
Syntax: class-name object1, object2,,objectn;
If fruit has been defined as a class, than the statement
fruit mango; fruit mango;
will create an object mango belonging to the class fruit.
Concepts of OOP:-
a. Object
b. Classes
c. Data abstraction and encapsulation
d. Inheritance
e. Polymorphism
f. Dynamic binding
g. Message passing

ii. What is the use of scope resolution operator? Explain with an example how a member
function is defined inside the scope and outside the scope of the class. (1 +2 +2 =5)
A scope resolution operator is used to identify the class to which a member function
belongs.
Inside the class definition:
When the function is defined inside a class, it is treated as inline function . Therefore, all
the restrictions and limitations that apply to an inline function are also applicable.
Normally, only small functions are defined inside the class definition.
class item
{
int number;
float cost;
public:
void getdata(int a, int b)// declaration
{
//body
}
void putdata()// inline function (definition inside the class)
{
cout<<\nnumber <<number ;
cout<<\ncost << cost ;
}
};
Outside the class definition:
Member functions that are declared inside a class have to be defined separately outside the class.
class sample
{
int m;
public:
void update();
void write();
};
void sample :: update()
{
read(); // simple call , no object used
}

iii. Explain the constructor with an example. What are the different types of a constructor?
What are the uses of copy constructor? (2 +2 + 1=5)
A constructor is a special member function whose task is to initialize the object of its class.
It is special because its name is the same as the 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.
A constructor is declared and defined as follows
//class with a constructor
class integer
{
int m, n;
public:
integer();


};
Here, integer() is a constructor. (see notes)

iv. Explain the inheritance with an example. Explain constructor and destructor in
inheritance. What are the uses of inheritance in OOP. (2 +2 + 1=5)
INHERITANCE: Inheritance is the process by which objects of one class acquire the
properties of objects of another class. It supports the concept of hierarchical classification.
In OOP, the concept of inheritance provides the idea of reusability. This means that we
can add additional features to an existing class without modifying it.
v. Explain the function overloading with examples of different numbers of arguments. 5
See notes
vi. What is this pointer? Explain with an example the address of an object.
See notes
vii. Write a C++ program to define a class having member data int arr[] and member functions
getdata( ), find( ) and display() to perform the following task:
I. To input 10 numbers from 0 to 10
II. To find out the odd numbers and even numbers
III. To display them separately
#include <iostream.h>
#include<iomanip.h>
#include<conio.h>
int j=0,k=0;
class number
{
int arr[10],odd[10],even[10];
int i;
public:
void getdata();
void find();
void display();
};
void number::getdata()
{ cout<<"\n Enter numbers\n";
for(i=0;i<10;++i)
cin>>arr[i];
}
void number::find()
{
for(i=0;i<10;++i)
{
if((arr[i]%2) !=0)
{
odd[j]=arr[i];
j++;
}
else
{ even[k]=arr[i];
k++;
}
}
}
void number::display()
{ if(j!=0)
{
cout<<"\nODD NUMBERS\n";
for(i=0;i<j;++i)
cout<<odd[i]<<"\t";
}
if(k!=0)
{
cout<<"\n\nEVEN NUMBERS\n";
for(i=0;i<k;++i)
cout<<even[i]<<"\t";
}
}

int main()
{

number obj;
obj.getdata();
obj.find();
obj.display();
getch();
return 0;
}
viii. Write a C++ program to define a class having two member data of type integer and
member functions getdata( ), swap( ) and display() to perform the following task:
I. To input two numbers
II. To exchange the values
III. To display the result
#include<iostream.h>
#include<conio.h>
class num
{
int x;
int y;
public:
void getdata();
void swap();
void display();
};
void num::getdata()
{
cout<<"Enter two numbers ";
cin>>x>>y;
}
void num::swap()
{
int temp;
temp = x;
x=y;
y=temp;
}
void num::display()
{
cout<<"\nX="<<x;
cout<<"\nY="<<y;
}
void main()
{
clrscr();
num n1;
n1.getdata();
cout<<"\nBefore swapping\t";
n1.display();
n1.swap();
cout<<"\nAfter swapping\t";
n1.display();
getch();
}


2011 (july)
1. Explain any 10 of the following terms: (1 x 10 = 10)
i. Arrays in C++
ii. Reserved words
iii. Tokens
iv. Typecasting
v. Global variables
vi. Comments
vii. C++ streams
viii. Member functions
ix. Recursion
x. Function prototype
xi. Pre-processor directives
xii. Identifiers
xiii. Compilers
xiv. Pointers
2. Explain any 10 of the following (2 x 10 = 20)
i. Class and object
ii. Basic data types supported in C++
iii. Static and dynamic variables
iv. Control statements in C++
v. Input streams and output streams
vi. Dynamic binding
vii. Superclass
viii. Virtual function
ix. Inline functions
x. Access specifiers
xi. Abstraction and encapsulation
xii. Actual and formal parameters
xiii. Call by value and call by reference
xiv. Default arguments

3. Answer any five (3 x 5 =15)
i. Procedure oriented programming and object oriented programming
ii. Structure and classes
iii. Inline and friend function
iv. Polymorphism and abstraction
v. Lifetime and scope of a variable
vi. Base and derived class
vii. Multiple and multilevel inheritance
4. Answer any Six
i. Write a C++ Program to perform the following tasks (5)
a. Area of a triangle
b. Area of a square
c. Area of a rectangle
Ans :
ii. What do you mean by constructor? What are the various types of constructors? Differentiate
between constructor and destructor. (1+2+2=5)
iii. Write a C++ Program to print the first Fibonacci numbers. (5)
iv. Write a C++ Program to print the factorial of an integer using recursion. (5)

v. What do you mean by inheritance? What are the various types of inheritance? Why is
inheritance needed? (1+3+1=5)
vi. Write a C++ Program to check whether a given string is a palindrome or not. (5)
vii. Define a structure with the following information: student name, roll, class. Using student
declare an array student with 20 elements and Write a C++ Program to read information for
20 students and print the lists of students with roll, name, class. (5)
viii. What do you mean by operator overloading? What are the various types of operator
overloading? What are the advantages of operator overloading? (1+2+2=5)
ix. Write a C++ Program to define a class to represent numbers (three float values) include
member functions to perform the following tasks (5)
a. To input three numbers
b. To find and display the highest number.
c. To display them separately.


2010(July)
OOP in C++
1. (a) Write a short note on OOP. (4)
(b) Write a program that generates the factorial of a number using function. (5)
(c) Write a program that generates the factorial of a number using function. (5)
2. (a) Explain parameterized constructor with suitable example. (4)
(b) What is a constructor? What is its need? Explain with the help of an example. (4)
(c) Give the salient features of destructors? (3)
(d) What is copy constructor? What do you understand by constructor overloading? (4)
3. (a) Explain the concept of inheritance. (4)
(b) Write a C++ program to read and display information about employees and managers. Employee is
a class that contains employee number, name, address and department. The class manager
contains all information of the class employee working under managers. (6)
(c) What is a virtual class? What is their significance? (2)
(d) Define base class and derived class. How are these related? (3)
4. (a) What is operator overloading? What are the benefits of operator overloading? Can any operator
in C++ be overloaded? (5)
(b) Explain step involved in operator overloading taking suitable example. (5)
(c) List the operators that cannot be overloaded in C++. Can the precedence of operator change when
it is overloaded? Explain. (5)
5. (a) What is polymorphism? How do you achieve polymorphism in C++? (4)
(b) List the rules to be followed in using virtual functions? (5)
(c) What are the advantages of virtual functions? What is pure virtual function? What is its
significance? (6)
(d) (a) What is a stream? Differentiate between input stream and output stream? (4)
(b) Explain stream class hierarchy. (6)
(c) What is manipulator? Why do we need manipulators? Explain the advantages of manipulators? (5)
(e) Write short note of the followings: (3 X 5 =15)
a. Dynamic binding
b. Encapsulation
c. Function overloading
d. Static data member
e. Pre-processor directives

2009 (JUNE)
1. All are compulsory.
a) Write two major differences between OOP and procedural programming? (2)
b) Reusability of classes is one of the major properties of OOP. How is it implemented in C++? (2)
c) What is the difference between global and local variables? Give an example to illustrate the same. (2)
d) How does a class enforce, hiding, abstraction and encapsulation? (2)
e) What do you understand by a default constructor? What is its role? How is it equivalent to a
constructor having default arguments? (3)
f) List the characteristics of a constructor. (3)
g) What is operator overloading? Can any operator in C++ be overloaded? (3)
h) What are friends? When do you need a friend operator? (3)
i) Distinguish between a base class and derived class? (2)
Group B
Answer any 5(five) from the following
2. a) Write a program to find the row sum and column sum of a matrix. (6)
b) Write a C++ program to print Fibonacci series i.e. 0112358. (4)
3. a) What do you mean by static data members of a class? Explain the characteristics of a static data
member. (2 + 3 = 5)
b) Explain the role of default constructor. When it is considered equivalent to a parameterized
constructor? (3 + 2 = 5)
4. a) What are pure virtual functions? (3)
b) What is static binding and dynamic binding? (3)
c) Can we declare a static function as virtual? Explain how. (2)
d) What is upcasting? (2)
5. a) What is a string? Name the string generally used for file input and output. (4)
b) Differentiate between function read( ) and write(). (2)
c) What is difference between getc( ) and getline( ) functions even when they share the similar
prototype? (4)
6. a) What are different forms of inheritance supported in C++? Explain them with appropriate
examples. (7)
b) When should one derive class publicly and privately (3)
7. a) Write a C++ program to create a base class called bird and derive from this class to a new class
called flying and non-flying consider name, color and country as data member of the class bird.(5)
b) What are the different types of file opening modes? (5)
8. Write short notes on the following (2 X 5 =10)
a) Data abstraction
b) Polymorphism
c) Virtual function
d) Friend function
e) Data hiding

The figures in the right margin indicate full marks for the questions.
Group - A
Answer question No. 1 and any four (4) questions from the rest of this group.
1. a) What is the difference between an object-based language and object-oriented language? (3)
b) What do you mean by abstraction? (2)
c) What is the purpose of abstraction? (2)
d) What is the purpose of polymorphism? (2)
e) What is a class and what is an object? (3)
f) Why nested comments are not allowed? (2)
g) What is anonymous union? What are the restrictions imposed on an anonymous union? (4)
h) When should we make a call by reference? (4)
i) Is reference a pointer? (1)
j) Is it safe to return a local variable by reference? Give reasons? (2)

2. a) What do you mean by function overloading? (3)
b) What do you mean by abstraction? (3)
c) When do we need to use default arguments in a function? (4)
3. a) What is a friend function? (5)
b) How do we invoke a constructor function? (2)
c) What is parameterized constructor? (2)
d) Can destructor take any arguments? (1)
4. a) What is Operator overloading? (1+2 =3)
b) How many arguments are required in the definition of an overloaded unary operator? (1)
c) What is a conversion function? How is it created? Explain its syntax. (6)
5. a) What are pure virtual functions? (3)
b) What is static binding and dynamic binding? (3)
c) Can we declare a static function as virtual? (2)
d) What is upcasting? (2)
6. a) What is a stream? (1)
b) What is the difference between the manipulator and setf( ) function? (6)
c) What are the three stream classes that are commonly used for disk I/O? (3)
GROUP -B
Choose any one from this group
7. a) What are the different types of file opening mode? (5)
b) What is the difference between text mode and binary mode while performing file I/O operations? (5)
8. Write a C++ program to search a word in a file and replace it with the specified word. The user should
be able to specify the old word and new word. (10)

Você também pode gostar