OOP Chapter 1
OOP Chapter 1
Chapter 1.
Principles of Object Oriented Programming
12mks
Syllabus:
1.1 Procedure Oriented Programming (POP) verses Object Oriented Programming (OOP)
1.2 Basic concepts of Object Oriented Programming, Object Oriented Languages, Applications of OOP.
1.3 C verses C++, Structure of C++ program, Simple C++ Program
1.4 Tokens, keywords, variables. Constants, basic data types, User defined data types, type casting,
operators, expressions.
1.5 Control structures: Decision making statements and Loop
1.6 Scope resolution operator, memory management operators. Arrays, Strings and Structures in C++
Q Difference between OOP and POP. (Any four points are expected, each point –1 Mark)
Ans:
Procedure oriented programming Object oriented programming
2. Large programs are divided into multiple 2. Programs are divided into multiple objects.
functions.
3. Data move openly around the system 3. Data is hidden and cannot be accessed by
from function to function. external functions.
4. Functions transform data from one form 4. Objects communicate with each other through
to another by calling each other. function.
Objects:
Objects are the 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. When a
program is executed, the objects interact by sending messages to one another.
Eg.
void main()
{
Employee e;
…
…
}
Inheritance:
Inheritance is the process by which objects of one class acquired the properties of objects of
another classes. In OOP, the concept of inheritance provides the idea of reusability.
Polymorphism:
Polymorphism means the ability to take more than on form. An operation may exhibit different
behavior is different instances.
Dynamic Binding:
Binding refers to the linking of a procedure call to the code to be executed in response to the
call.
Message Passing:
An object-oriented program consists of a set of objects that communicate with each other.
Objects communicate with one another by sending and receiving information. Example:
Employee.Salary (name);
Q State any four applications of OOP. (Any four applications; each carry - 1 Mark)
Ans:
Applications Object oriented programming are as follows
Real time systems
Simulation and modeling
Object oriented databases
Hypertext, hypermedia and expertext
AI and expert systems
Neural networks and parallel programming
Decision support and office automation systems
CIM/CAM/CAD systems.
Q With suitable diagram describe structure of C++ program. (Diagram - 1 Mark; Description -
3 Marks)
Ans:
General C++ program has following structure.
INCLUDE HEADER FILES
DECLARE CLASS
DEFINE MEMBER FUNCTIONS
DEFINE MAIN FUNCTION
Description:-
1. Include header files
In this section a programmer include all header files which are require to execute given program. The
most important file is iostream.h header file. This file defines most of the C++ statements like cout
and cin. Without this file one cannot load C++ program.
2. Declare Class
In this section a programmer declares all classes which are necessary for given program. The
programmer uses general syntax of creating class.
Q Define class with its syntax. (Class definition – 1 Mark, Syntax – 1 Mark)
Ans:
Definition: A class is a way of binding data and its associated functions together.
Syntax:
class class_name
{
private:
Variable declarations;
Function declarations;
public:
Variable declarations;
Function declarations;
};
Q Describe syntax of ‘cin’ and ‘cout’ with example. (cin syntax - 1 Mark, example - 1 Mark; cout
syntax - 1 Mark, example - 1 Mark)
Ans:
• cin = cin statement is use to accept input from user.
Syntax:-
cin>>variable_name;
Example:
cin>>i;
• cout = cout statement is use to display output processed by computer or to display simple
messages to user.
Syntax:-
cout<<variable_name;
cout<<”Message”;
Example:
cout<<”Hello world”;
cout<<x;
Extraction operators: The operator >> is known as extraction or get from operator. It extracts (or takes)
the value from the keyboard and assigns it to the variable on its right.
Example: cin>> number1;
Q What is the significance of scope resolution operator? (Use of scope resolution operator – 2
Marks)
Ans: Scope resolution operator allows access to the global version of a variable, and also allows
facility to define member functions outside of a class.
It is represented with double colon (:: ).
Q. Write a program to declare a class ‘employee’ having data members name and age.
Ans:
Accept and display the data for three objects.
#include<iostream.h>
#include<conio.h>
class Emp
{
private:
int sal;
char name[20];
static float tot;
public:
void getdata(); //Member function declare inside class
void putdata(); //using scope resolution operator
void show();
};
void Emp::getdata() //Member function define outside class
{ //using scope resolution operator
cout<<"Enter name:";
cin>>name;
cout<<"salary";
cin>>sal;
}
e1.putdata();
e2.putdata();
e3.putdata();
e1.show();
getch();
}
Q List two memory management operators available in C++ and state its use in one line. (1-
Mark for new; 1- Mark for delete)
Ans:
Q How memory is allocated to the objects of class, explain with diagram (Description - 3 Marks;
diagram - 1 Mark)
Ans:
Memory space for objects is allocated when they are declared. The member functions are created
and placed in the memory space only once when they are defined as a part of class specification.
Since all the objects belonging to that class use the same member functions, no separate space is
allocated for member functions when the objects are created.
Only space for member variables is allocated separately for each object.
Separate memory locations for the objects are essential, because the member variables will hold
different data values for different objects.
Q. Give syntax and example of defining structure and declaring structures variables.
Ans:
Definition:-
Structure is a collection of different data types written under a common name. It is a user defined data
type
Example:
struct book OR
{ struct book
char book_name[10]; {
char author_name[10]; char book_name[10];
float price; char author_name[10];
}b; float price;
};
void main()
{ void main()
b.book_name; {
b.author_name; struct book b;
} b.book_name;
b.author_name;
}
Q Differentiate between structure and class. (Any four points of comparison - 1 Mark each)
Ans: