You are on page 1of 19


Q-1 Compare procedural language and object oriented language for solving problems. What are their advantages and disadvantages?
Procedural Language (Ex: FORTRAN, Pascal ,C, ALGOL, Ada) :

Emphasis is on algorithms Large programs are divided into smaller programs known as functions. Most of the function share common data. Data move openly around the system from function to function. Functions transform data from one form to another. Employs top-down approach.

Object Oriented Language (Ex: C++, Java)


Emphasis is on data rather than procedure Large programs are divided into what are known as objects Data structures are designed such that they characterize the objects. Functions that operate on the data of an object are tied together in the data structure. Data is hidden and cannot be accessed by external functions. Objects may communicate with each other through functions. New data and functions can be easily added whenever necessary. Follows bottom-up approach in program design.

Advantages of Procedural Language: include its relative simplicity, ease of implementation. Procedural languages were important in history for their low memory utilization. Disadvantages of Procedural Language: include difficulties of reasoning about programs and to some degree difficulty of parallelism. Procedural programming tends to be relatively low level compared to some other paradigms, and as a result can be very less productive. Advantages of Object Oriented Language: OOP provides a clear modular structure for programs which makes it good for defining abstract data types where implementation details are hidden and the unit has a clearly defined interface. OOP makes it easy to maintain and modify existing code as new objects can be created with small differences to existing ones. OOP provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.

Disadvantages of Object Oriented Language:

Not all programs can be modeled accurately by the objects model. If you force the language into the OOP concept, you lose some of the features of useful languages like the "functional languages." The objects often require extensive documentation.

Q-2 What is OOP? Explain the features/basic concepts of an object oriented programming with suitable examples. Object Oriented Programming (OOP) is the programming paradigm uses objects to design applications and computer programs.
Features of OOP include: 1) Objects: Objects are the basic run-time entities in an object oriented system. The basic unit of Object Oriented Programming. Objects are data structures consisting of data fields and methods. Each object is a particular instance of a class, which has its unique name. Each object carries the properties of its relevant class, i.e.- variables & associated functions. When created, each object allocates memory for variables and data associated with it. 2) Classes: Classes are the data types from which objects are created. It can be seen as a template which defines the abstract characteristics and behavior of a thing (object). a. Properties Characteristics of each object that may be created from the class. b. Methods Actions to be performed by the objects that are declared in class. No memory is allocated when a class is created. Memory allocation happens only when the actual instances of a class (the objects) are created. Basically, a class is an expanded concept of a data structure; which can hold data as well as functions. In terms of variables, a class would be the data_type and the object would be the variable. 3) Data Abstraction: It allows creation of user-defined data types. It represents the needed information without presenting the details. The main idea behind data abstraction is to give a clear separation between properties of data type and the associated implementation details. - Abstract data type: 1. Properties clearly visible to the user interface. 2. Implementation details hidden. 3. Abstract data type defined in terms of the operations that it performs, and not in terms of its structure (implementations). Reasons for need of abstraction: Flexibility in approach: By hiding data or abstracting details, the programmer achieves greater flexibility.

Enhanced security: Hiding the implementation details provides security. Easier replacement: Allows the code to be replaced without recompilation. So, replacing code becomes easier and less time consuming. Modular approach: Project application may be divided into modules and each of them may be tested separately. Later, all the modules are taken together and tested. So, application development approach becomes easier. 4) Encapsulation: The process of combining or packaging data with functions and thereby allowing users to create a new abstract data type. (which is nothing but a class!) As data and functions reside inside a single autonomous unit. The user cannot directly access the encapsulated data. Encapsulation introduces the concept if information hiding. As the data and the functions are bundled inside the class, the class takes total control of maintenance and hence human errors are reduced. Encapsulated objects act as black boxes to the other parts of the program. This enhances the security. And still, the objects maybe used to achieve some functionality. The usage of access specifiers can further add restrictions on the way data may or may not be allowed to be accessed. In case a user wants some non-member function to access an objects private or protected data, he/she can use friend functions. 5) Inheritance: A process of forming a new class from an existing class (often called base class). The base class also called parent class/super class. The derived class also called as child class/sub class/extended class. Inheritance helps reduce the overall size of the program code, which helps the overall performance. The derived/extended classes have all the properties of the base class. Upon them, a programmer can choose to add new features specific to the newly created derived class. Advantages of inheritance: Reusability Inheritance helps the reuse of the same code . Once the base class is defined and worked out, it need not be reworked. Any no. of derived classes can be created from the base class as needed while adding specific features to each derived class as needed. Saves Time & Efforts Increases the program structure which results in a greater reliability. 6) Polymorphism: Polymorphism allows routines to use variables of different types at different times. It is an ability to use an operator or a function in different ways. A function given different meanings or an operator functioning in many ways is called Polymorphism.

There are two types of polymorphism 1) Run time 2) Compile time Run-time polymorphism can be implemented using virtual functions. Overloading (compile time) is a type of polymorphism. When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded. There are two types of overloading: Function overloading, & Operator overloading Advantages of polymorphism include: Application are easily extendable Provides easier maintenance of applications. Helps in achieving robustness in applications.

7) Dynamic Binding: Dynamic Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding is also known as late binding, means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance. A function call associated with a polymorphic reference depends on the dynamic type of the reference. 8) Message Passing: An OOP consists of a set of objects that communicate with each other. The process of programming in an object oriented language, therefore involves following basic steps:

Creating classes that defines objects and their behavior. Creating objects from class definition and Establishing communication among objects.

Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. The concept of message passing makes it easier to talk about building systems that directly model or simulate their real world counter parts. Objects have a life cycle. They can be created and destroyed. Communication between objects is feasible as long as it is alive.

Q-3 Difference between C and C++

C is Procedure Oriented Programming Language (POP). C is mostly used to develop system software. C program has extension .C C++ is Object Oriented Programming Language (OOP). C++ is mostly used to modal real life problem to program and use to develop application programs. C++ program has extension .cpp

C uses the top-down approach. C is function-driven. C does not allow the use of reference variables The NAMESPACE feature is absent in case of C C does not support Exception Handling Inheritance, polymorphism, Encapsulation features are not available in C

C++ uses the bottom-up approach. C++ is object-driven. C++ allows the use of reference variables The NAMESPACE feature is present in C++ C++ supports Exception Handling Inheritance, polymorphism, Encapsulation features are available in C++

Q-4 Visibility modifiers/Access specifier/Visibility Labels of C++ OR Explain public, private or protected access specifier.
a) public: All public type members of the class are accessible from outside the class. b) private: None of the members of type private within the class may be accessed from outside the class. c) protected: protected access specifier lies in between public and private. If the member function/variable is defined in a class as protected, it cannot be accessed from outside the class but they can be accessed by the derived class of that class.

Q-5 Explain with suitable example how scope resolution operator is used to access global version of variable.
In C, the global version of a variable cannot be accessed from within the inner block. C++ resolves this problem by introducing new operator :: called scope resolution operator. This can be used to uncover a hidden variable. It takes the following form: :: variable-name This operator allows access to global version of variable For example, ::count means the global version of the variable count. PROGRAM:

#include<iostream.h> using namespace std; int m = 10; int main() {

int m = 20; { int k = m; int m = 30; cout<< we are in inner block \n. cout<< m = <<m << \n; cout<< k = << k << \n; cout<< ::m = << ::m << \n; } cout<<we are in outer block\n; cout<< m = <<m << \n; cout<< ::m = << ::m << \n; } OUPUT: we are in inner block m = 30 k = 20 :: m = 10 we are in outer block m = 20 :: m = 10

Q-6 Explain Static Member Functions.

A member function that is declared static is known as static member function. It has following properties: A static function can have access to only static members declared in the same class. A static member function can be called using the class name as follows: class-name :: function-name ;

#include<iostream.h> using namespace std;

class test { int code; static int count; public:

//static data members

void setcode(void) { code = ++count; } void showcode(void) { cout<<Object number :<< code << endl; } static void showcount() //static member function { cout<<count : << count << \n; } }; int test :: count; int main() { test t1,t2; t1.setcode(); t2.setcode(); test :: showcount(); test t3; t3.showcode(); test :: showcount(); t1.showcode(); t2.showcode(); t3.showcode(); return 0; }

OUTPUT: count: 2 count: 3 object number : 1 object number : 2 object number : 3

Q-7 Explain Constructor and Destructor with example.

Constructor: Constructors are used to initialize the objects. Constructor is a special kind of function which is the member of the class. The name of the constructor is same as the name of the class. Constructor is automatically called whenever an object is created. Constructors DO NOT have a return type. A default constructor is the one with no parameters. If no constructor is defined by the user, the compiler supplies the default constructor. Types of constructor include:
1) 2) 3)

Default Constructor Parameterized Constructor Copy Constructor

Destructor: A destructor is the complement of the constructor that is, it is used to destroy the objects. The objects are destroyed in order to deallocate the memory occupied. The name of the destructor is the same as the constructor and is preceded by a ~ (tilde) operator. A destructor for objects is executed in the reverse order of the constructor functions. Program for constructor and destructor

#include<iostream> using namespace std; class cube { public: double side; //Member variable double volume() { return( side * side * side ); } cube(double side1) //Parameterized Constructor {

cout << "A constructor is called" << endl; side=side1; } cube() // Default Constructor { cout << "A default constructor is called " << endl; } ~cube() //Destructor { cout << "Destructing " << side << endl; } }; //class complete int main() { cube c1(2.34); cube c2; cout << "The side of the cube is: " << c1.side << endl; cout << "The volume of the first cube is : " << c1.volume() << endl; cout << "Enter the length of the second cube : " ; cin >> c2.side; cout << "The volume of second cube is : " << c2.volume() << endl; return(0); }

Q-8 Explain Inheritance? What are different types of inheritance supports C++? Note: (for Inheritance definition refer Q-2)
Types of inheritance 1. Single Inheritance : When a class derives from a single base class it is known as Single Inheritance. A (parent class) -> B (child class) 2. Multiple Inheritance: Multiple inheritance is that in which one or more classes derive from more than one base class it is known as Multiple Inheritance. A -> C, B -> C 3. Hierarchical inheritance: Hierarchical Inheritance is that inheritance in which there is a single Base class and multiple derived classes are derived from this Base class. A -> B, A -> C, A -> D

4. Multilevel inheritance: It is that inheritance in which a class derives from a class which is a derived class already. A -> B, B -> C 5. Hybrid inheritance: Hybrid inheritance is a combination of two or more types of inheritance. A -> B, A -> C, B -> D, C -> D. Advantages of Inheritance: Allows the code to be reused as many times as needed. The base class once defined and once it is compiled, it need not be reworked. Saves time and effort as the main code need not be written again.

Q-9 State whether the given statements are true or false:

1) Constructor, like other member functions can be declared anywhere in the class: FALSE 2) Constructor does not return any value : TRUE 3) Destructor never takes any arguments : TRUE 4) Constructor cannot be virtual : TRUE

5) Destructor can be virtual : TRUE PROGRAMS P-1 Write a C++ program to find the average of five numbers.
#include<iostream.h> using namespace std; int main() { float n1, n2, n3, n4, n5, sum ,avg; cout<<Enter five numbers \n; cin>>n1; cin>>n2; cin>>n3; cin>>n4; cin>>n5; sum = n1 + n2 + n3 + n4 + n5; avg = sum / 2; cout<<Sum = <<sum<<\n; cout<<Average of five numbers = <<avg <<\n; return 0; }

P-2 Write a C++ program to implement the concept of inheritance with suitable examples. OR Explain with a suitable example how code reusability is achieved in C++.
#include<iostream.h> using namespace std; class B { int a; public: int b; void set_ab(); int get_a(void); void show_a(void); }; class D : public B { int c; public: void mul(void); void display(void); }; //---------------member function implementation-------------------------------------------------------void B :: set_ab(void) { a = 5; b = 10; } int B :: get_a() { return a; } void B :: show_a() { cout<<a = << a << \n;

} void D :: mul() { c = b * get_a(); } void D :: display() { cout << a = << get_a() << \n; cout << b = << b << \n; cout << c = << c << \n; } //---------------main function---------------------------------------------------------------------int main() { D d; d.set_ab(); d.mul(); d.show_a(); d.display(); d.b = 20; d.mul(); d.display(); return 0; } OUTPUT a=5 a=5 b = 10 c = 50 a=5 b = 20

c = 100

P-3 Explain multiple inheritance. Elaborate your answer with suitable example.

using namespace std; class M { protected: int m; public: void get_m(int); }; class N { protected: int n; public: void get_n(int); }; class P : public M, public N { public: void display(void); }; //---------------member function implementation--------------------------------------------------------void M :: get_m(int x) { m = x; } void N :: get_n(int y) { n = y; } void P :: display(void) { cout<< m = << m << \n; cout<< n = << n << \n; cout<< m * n = << m*n << \n; }

//---------------main function---------------------------------------------------------------------int main() { P p1; p1.get_m(10); p1.get_n(20); p1.display(); return 0; }

P-4 Write a C++ program to add two complex numbers. OR Write a C++ program to implement the concept of polymorphism.
Note: Here two complex numbers are added using operator overloading (polymorphism) #include<iostream.h>

using namespace std; class complex { float x; float y; public: complex() { }; complex(float real, float img) { x = real; y = img; } complex operator+(complex); void display(void); }; //---------------member function implementation--------------------------------------------------------complex complex :: operator+(complex c) { complex temp;

temp.x = x + c.x; temp .y = y + c.y; return(temp); } void complex :: display(void) { cout<< x << + j << y << \n; } //---------------main function---------------------------------------------------------------------int main() { complex c1, c2, c3; c1 = complex(2.5, 3.5); c2 = complex(1.6, 2.7); c3 = c1 + c2; cout << c1 = ; c1. display(); cout << c2 = ; c2. display(); cout << c3 = ; c3. display(); return 0; } OUTPUT: C1 = 2.5 + j3.5 C2 = 1.6 + j2.7 C3 = 4.1 + j6.2

ssssP-5 What is the advantage of encapsulation in object oriented program. Explain with a suitable example. Note: (for advantages of Encapsulation refer Q-2)
EXAMPLE PROGRAM: #include<iostream> using namespace std; class programming { private: int variable; public: void input_value() { cout << "In function input_value, Enter an integer\n"; cin >> variable; } void output_value() { cout << "Variable entered is "; cout << variable << "\n"; } }; main() { programming object; object.input_value(); object.output_value(); //object.variable; Will produce an error because variable is private return 0; }

P-6 Write a C++ program for following inheritance diagram.

Employee Data Members: Name Number getdata() putdata()

Manager Data Members: Title Department getdata() putdata()

Scientist Data Members: No_of_publications

getdata() putdata()

#include<iostream.h> using namespace std; class Employee { protected: char name[50]; int number

}; class Manager : public Employee { char title[50]; char Department[50]; public: void getdata(); void putdata(); }; class Scientist : public Employee { int No_of_publications; public: void getdata(); void putdata(); }; //---------------member function implementation-------------------------------------------------------void Manager :: getdata() { cout<< Enter manager details; cin>>name>>number>>title>>department; } void Manager :: putdata() { cout<< Manager details are:; cout<<Name: << name; cout<<Title: << title; cout<<Number: << number; cout<<Department: << department; } void Scientist :: getdata() { cout<< Enter scientist details; cin>>name>>number>>title>>department;

} void Scientist:: putdata() { cout<< Scientist details are:; cout<<Name: << name; cout<<Number: << number; cout<<No of Publications: << No_of_publications; } //---------------main function---------------------------------------------------------------------int main() { Manager M; Scientist S; M.getdata(); M.putdata(); S.getdata(); S.putdata(); return 0; }