Oops
Oops
Oops
15/22 IInd Floor Ashok Nagar, New Delhi-110018. Ph.: 25499279, 55711031(O), 9810865706(M)
Class: XII C++ Basics of Object Oriented Programming (OOPS)
1. OOP. Object Oriented Programming enables the programmers to group together data and the code
that uses data into discrete units. In OOP the stress is given on object in spite of procedure.
2. Procedural Programming. In procedural programming, we think about functions and the execution
flow through these functions.
3. Object. An object is a self contained abstraction of an item. An instance of a class.
4. Class. A class is user defined data type used to implement an abstract object. It contains members of
three types: private, public and protected. By default the data members of the class and private.
Classes enable the programmer to model objects with attributes and behaviours. Class type can be
defined in C++ using the keyword class and struct, but keyword class is normally used for this
purpose.
5. Class declaration. The class is declared with a keyword class:
class <class_name>
{
private :
data_members;
member_functions;
[public :
data_members;
member_functions;
protected:
data_members;
member_functions;]
};
6. Members. There are two kinds of members in the class. First, data members and the second and
members functions. Member access specifiers always end with a colon (;) and can appear multiple
times and in any order in a class definition.
7. Data members. Data members are exactly like the variables in a structure. Data members of a class
are normally made private and members functions of a class are normally made public. Some
member functions may be private and serve as utility functions to the other functions of the class.
8. Private section. Private members are available to only members of class and not accessible to
functions outside the class. The default access mode for classes is private so all members after the
class header and before the first member access specifiers are considered to be private.
9. Public section. Any member following public keyword can be accessed from outside the class.
public members of a class present a view of the services the class provides to the clients of the class.
10. Scope resolution operator (::). This operator is used in situations where a global variable exists
with the same name as a local variable. Also, this is used in C++ class when the member functions
are declared outside the class definition; the function name is preceded by the class name and the
binary scope resolution operator (::).
11. Data hiding. The variables and functions declared in a class as private are not accessible to any
outside function. This feature of class declaration is called data hiding.
12. Abstract data type. The data type, which ties the data and its associated function into a single unit,
is called Abstract Data Type (ADT). Otherwise, Abstract Data Types are the data types, which
represent the required features of an item without including any background details, e.g.; class.
13. Encapsulation. Combining data and the required procedures to manipulate this data into a single
entity is known as encapsulation. This is an essential concept of OOP’s which is implemented
through classes.
14. Abstraction. The class member which are private remains hidden from outside world, the public
members from the interface by providing the essential and relevant information to outside world.
Thus only the essential features are represented to outside world without including the background
details, which is called abstraction.
SPECTRUM STUDY CIRCLE (The Acme of Excellence) 2
15/22 IInd Floor Ashok Nagar, New Delhi-110018. Ph.: 25499279, 55711031(O), 9810865706(M)
Class: XII C++ Basics of Object Oriented Programming (OOPS)
15. Inline functions. A function is defined as being inline, its implementation is substituted into the
code where the function call was made.
16. Friend function. A function outside of a class can be defined to be a friend function by the class
which gives the friend, free access to the private members of the class.
BASIC CONCEPT
1. OOP CONCEPT
(i) DATA – ABSTRACTION: It is the act of representing the essential features with including
the background details and explanations.
(ii) DATA – HIDING: It is related concept to data abstraction. Unessential features or
background details are hidden from the world
(iii) DATA – ENCAPSULATION : Wrapping up of data and associated functions in one single
unit. Encapsulation implements abstraction also.
(iv) INHERITANCE : It is capability of one class to inherit properties from another class. The
class which is inherited is base class and known as SUPER CLASS, and the class which
inherits from other class is called derived class or subclass.]
(v) POLYMORPHISM : It is ability for a message or data to be processed in more than one
form. Polymorphism is a property by which the same message can be sent to objects of
several class.
(vi) OBJECT : It represents an identifiable entity with some characteristics and behaviour.
• ELECTRIC SWITCH BOARD (from real world entity) is example, where the internal
connections like wiring etc are not shown, hence it just shows the essential features with
some abstraction.
• Real world objects have Physical characteristics (State) and behaviour (eg) a motor bike
has
Characteristics → Current gear, wheels etc
Behaviour → braking, accelerating, charging years etc
• In Software objects, state is maintained through variables or data items and behaviour is
implemented through functions (known as methods)
abstraction
Real world attribute {data, data, - - - - - - }
entity
• A class binds together data and its associated functions under one unit and hence
implement encapsulation, as encapsulation means wrapping up data and associated
functions together into a single unit.
• A class groups its members into three sections : private, protected and Public: The private
and protected members remain hidden from outside world. Thus through private and
protected members, a class enforces data-hiding.
A class is defined as
Class < class name >
{
private : // hidden data members / methods here
protected : // (unimportant implementation details)
Public : // exposed important details
};
* BENEFITS OF ENCAPSULATION
2. INFORMATION :* An object has a public interface for other objects to communicate with
HIDING * An object can maintain private information and methods that can be changed
without affecting other objects.
* WHEN YOU WANT TO CHANGE GEARS ON MOTOR BIKE, YOU DON’T NEED TO KNOW
HOW A METHOD IS IMPLEMENTED, YOU JUST NEED TO KNOW WHICH METHOD TO
INVOKE AND ITS INTERFACE WITHOUT NEED TO KNOW HOW A METHOD IS
IMPLEMENTED
* INHERITANCE, is implemented by specifying the name of the BASE CLASS FROM WHICH THE
CLASS BEING DEFINED (the derived class) has to inherit from.
Class < derived class name > : < base class name >
{
← derived class own features
}
* GRAND FATHER
SON
SPECTRUM STUDY CIRCLE (The Acme of Excellence) 4
15/22 IInd Floor Ashok Nagar, New Delhi-110018. Ph.: 25499279, 55711031(O), 9810865706(M)
Class: XII C++ Basics of Object Oriented Programming (OOPS)
* SHAPE → is ABSTRACT CLASS, it suggests a state like colour, size etc. It does not specify how to
implement interface like surface area, volume, perimeter etc.
FROM SHAPE CLASS, OTHER Classes like circle class, rectangle class, triangle class inherit.
These classes are CONCRETE CLASSES, As they provide proper functionality for its members
functions
Draw-circle ( ) Draw-Rectangle ( )
Draw-square ( )
Consider,
void see_day FUNCTION 1
{
cout << “can see through Daylight” ;
}
void see_night ( ) FUNCTION 2
{
cout << “can’t see through darkness” ;
}
In such cases, it is to be decided upon which function should be executed, for example
if (choice = = ‘D’ )
see_day ( ) ; // calling of functn1
else
if (choice = = ‘N’)
see_night ( ) ; // calling of functn2
SPECTRUM STUDY CIRCLE (The Acme of Excellence) 5
15/22 IInd Floor Ashok Nagar, New Delhi-110018. Ph.: 25499279, 55711031(O), 9810865706(M)
Class: XII C++ Basics of Object Oriented Programming (OOPS)
Example of FUNCTION-OVERLOADING
1. RE-USE OF OOP
2. Ease of Comprehension
3. Ease of fabrication and maintenance
4. Easy redesign and extension
ASSIGNMENT
Q1. How are data and functions organized in Object Oriented Program?
Q2. Identify the error(s) in the following code-fragment:
class X { int a b;
void count (void)
{ a++ ;
}
public :
int X,
void init (int, int, int);
void print (void);
};
void X :: init(int i, int j, int k)
{ a = i;
b = j;
x = k;
void X :: print (void)
{ count ( );
cout << “a =” << a ; << “ b =” << b<< “ x =” << x << “\n” ;
}
void func(void);
X 01;
int main ( )
{ X 02;
01.init (0, 1, 2);
02.init (2, 3, 4);
SPECTRUM STUDY CIRCLE (The Acme of Excellence) 6
15/22 IInd Floor Ashok Nagar, New Delhi-110018. Ph.: 25499279, 55711031(O), 9810865706(M)
Class: XII C++ Basics of Object Oriented Programming (OOPS)
Q5. Define a class to represent a book in a library. Include the following members :
Data Members
Book Number, Book Name, Author, Publisher, Price, No. of Copies, No. of copies issued
Member Functions
(i) To assign initial values (ii) To issue a book after checking for its
availability
(iii) To return a book (iv) To display book information.
Q6. Declare a class to represent fixed-deposit account of 10 customers with the following data members:
Name of the depositor, Account Number, Time Period (1 or 3 or 5 years), Amount. The class also
contains following member functions:
(a) To initialise data members.
(b) For withdrawal of money (after half of the time period has passed).
(c) To display the data members.
Q7. Define a class to represent batsmen in a cricket team. Include the following members ;
Data Members
First name, Last name, Runs made, Number of fours, Number of sixes,
Member Functions
(i) To assign the initial values
(ii) To update runs made
(It should simultaneously update fours and sixes, if required).
(iii) To display the batsman’s information.
Make appropriate assumptions about access labels.
SPECTRUM STUDY CIRCLE (The Acme of Excellence) 8
15/22 IInd Floor Ashok Nagar, New Delhi-110018. Ph.: 25499279, 55711031(O), 9810865706(M)
Class: XII C++ Basics of Object Oriented Programming (OOPS)
Q8. Define a class to represent bowlers in a cricket team. Include the following members :
Data Members
First name, Last name, Overs bowled, Number of Maiden overs, Runs given, Wickets taken.
Member Functions
(i) To assign the initial values
(ii) To update the information
(iii) To display the bowler’s information
Make appropriate assumptions about access specifies.
Q9. Write a program to manage a room’s statistics. The room class includes the following members:
Data Members
Length, Width, Height
Member Functions
(i) To assign initial values (ii) To calculate area
(iii) To display information (length, width, height & area)
Q10. Modify the above program so that length, width and height become objects of class distance that
includes:
Data Members
Meters, Centimeters
Memory Functions
(i) To assign values (ii) To print values.
Q11. Declare a class to represent bank account of 10 customers with the following data members. Name of
the depositor, Account number, Type of account, (S for Savings and C for Current). Balance account.
The class also contains members functions to do the following:
(i) To initialize data members
(ii) To deposit money
(iii) To withdraw money after checking the balance (minimum balance in Rs. 1000)
(iv) To display the data members
[Note: You are also required to give detailed function definitions.]
public members:
Readdata( ) function accepts the data values and invoke the calculate function
Displaydata( ) function prints the data on the screen.
Q17. Declare a class to represent bank account of 10 customers with the following data members. Name of
the depositor, Account number, Type of account, (S for Savings and C for Current), Balance amount.
The class also contains member functions to do the following:
(i) To initialize data members (ii) To deposit money
(iii) To withdraw money after checking the balance (minimum balance in Rs. 1000)
(v) To display the data members
Note: You are also required to give detailed function definitions.
void main( )
{ Counter C1, C2 ;
cout << “\n C1 =” << C1 get_Count( ) ;
cout << “\n C2 =” << C2.get_Count( ) ;
C1.inc_Count( ) ;
C2.inc_Count( ) ;
C2.inc_Count( ) ;
cout << “\nC1 =” << C1.get_Count( ) ;
cout << “\nC2 =” << C2.get_Count( ) ;
}
}
SPECTRUM STUDY CIRCLE (The Acme of Excellence) 13
15/22 IInd Floor Ashok Nagar, New Delhi-110018. Ph.: 25499279, 55711031(O), 9810865706(M)
Class: XII C++ Basics of Object Oriented Programming (OOPS)