object oriented programming using c++
DESCRIPTION
This Powerpoint presentation covers following topics of C Plus Plus: Features of OOP Classes in C++ Objects & Creating the Objects Constructors & Destructors Friend Functions & Classes Static data members & functionsTRANSCRIPT
Using C++
Object Oriented Programming
Contents
Features of OOPClassesObjects & Creating the ObjectsConstructors & Destructors Friend Functions & ClassesStatic data members & functions
OOP
It is programming technique in which programs are
written on the basis of objects
It is a powerful technique to develop software.
It is used to analyze and design the application in terms of
objects.
It deals with data and the procedures as a single unit
Interacting objects handle their own house-keeping.
Objects in a program interact by sending messages to each
other.
Each object is responsible to initialize and destroy itself.
There is no need to explicitly call a creation or termination
procedure
Features of object-oriented programming
Data abstraction the procedure to define a class from objects.
EncapsulationA technique for Information Hiding.
InheritanceIt allows to define a class in terms of another class, which makes it easier to create and maintain an application.
Dynamic bindingIt determining the method to invoke at runtime instead of at compile time
Polymorphism The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
Effects of OO methodology on software design
Maintenance
Extensibility
Reusability
Objects
Object represents an entity in the real world Identified by its name It consists of two things:
Properties: Characteristics of an object Functions Actions performed by the
object
o Everything is an objecto Systems are composed of objects
Everything is an object A student, a professor A desk, a chair, a classroom, a building A university, a city, a country The world, the universe A subject such as CS, IS, Math, History, …
Systems are composed of objects An educational system An economic system An information system A computer system
Design Methodologies
Object-Orientation is a design methodology
Objects are the building blocks of a program
(interface, editor, menu, file, etc.); data
managing object (db), etc.)
Objects represent real-world abstractions
within an application.
Properties of Objects
Characteristics of an object are known as Properties or attributes of the object
Each object has its own propertiesExample:If “Person” is an object, it has following properties
Name Age Weight
Object: CarProperties: Model, Color, Price
Functions of an Object
Tasks or actions performed by the object are
known as functions or methods.
Classes
Collection of objects with same
properties and functions
Use to define characteristics of the
object
Used as a model for creating
different objects of same type
Each object of a class is known as
an instance of the class
Declaring a class
Keyword “class” is used to declare a classDeclaration specifies:
Member Variable / Data member Function / Member FunctionThese are common to all objects of that class
Syntax:class identifier{Body of the class};Class: is the keywordIdentifier: name of the class to be declared
Access Specifiers
It specifies the access level of the class members
Two common access specifiers are: Private:
Restrict the use of the class members within the class. It is the default access specifier. It is used to protect the data members from direct access from outside the class. Data Member are normally declared with private access specifier.
Public It allows the user to access members within the class as well as outside the class. It can be accessed from anywhere in the program. Member functions are normally declared with public access specifier.
Creating objects
Class is simply a model or prototype for creating objects.
It is like a new data type that contains both data and functions.
Object is created in the same way as other variables are created.
Object is also known as instance of a class.Process of creating an object is also called
instantiation.Syntax:class_name object_name;Class_name: name of the class whose type of object is to be createdObject_name: object to be created.
Executing Member Functions
Member functions are used to manipulate data members of a class.
Member functions can be executed only after creating objects
Syntax:Object_name.function();
Object_name: name of object whose member function is to be executed
Function: It is the member function that is need to be executed.
Write a program that declares a class with a data member
and two member functions
OUTPUT:
enter number 10the value of n= 10
Defining member functions outside class
Function declaration is specified within the class Function definition is specified outside the classScope resolution operator :: is used in function declaration if
the function is defined outside the class.
Syntax:Return_type class_name :: function_name(parameters){function body}
Return_type type of value to be returned by functionclass_name class name to which function belongs :: scope resoltion operatorfunction_name name of funtio to be defined
Constructors
Type of member function that is automatically executed when an object of that class is created is known as constructor
It has no return typeIt has same name that of class nameIt work as normal function but cannot return any valueIt is used to initialize data memebrs
Syntax: name(){Constructor body}Name: it indicate the name of the constructor
Passing parameters to constructor
It is same as passing parameters to normal functions
Only difference is Parameters are passed to the constructor when the
object is declared.
Syntax:type object_name(parameters);
Type: it is the name of the class (type of the object to be declared)Object_name: name of the object to be declaredParameter: list of parameters passed to the constructor
Constructor overloading
Declaring multiple constructors with the
same name but different parameters
It must differ in one of the following ways
Number of parameters
Type of parameter
Sequence of parameters
Output
the constructor of first= num = 0ch = xthe contents of second = num = 100ch = p
Default copy constructor
It is available by default in all classesIt is used to initialize an object with another object of the
same type.User does not need to write this constructor It accepts a single object of the same type as parameter.
Syntax: Class_name object_name(parameter); ORClass_name object_name = parameter;
Class_name: type of object to be createdObject_name: name of the objectParameter: name of parameter passed to default constructor
Destructors
Member function that is automatically executed when an object of that class is destroyed in known as destructor
Is has no return typeName is same as the classIt also cannot accept any parameterConstructor name proceeded by tilde sign ~
Syntax: ~name(){destructor body}
Objects as function Parameters or Return Type
As parameters:Objects can also be passed as parameters to
member functionsMethod is same as passing parameters to other
functions
As return type:Returning an object from member function is
same as returning a simple variableIts return type should be the same as the return
type of the object to be returned.
Static data member
The type of data member that is shared among all the objects of the class is known as static data members.
Defined with static keywordIf defined static member; only one variable is created
in memory even if there are many objects of that classUsed to share some data among all objects of a
particular classVisible only in the class in which it is definedIts lifetime:
Starts when the program starts its execution Ends when the entire program is terminated
Difference between normal and static data members
A
B
N
A
B
A
B
A
B
N
1 2
10
10010
1
200
20
1
1
200n
Object b1 Object b2 Object b2Object b1
Three normal data members
Two normal data members (a,b) and one static member (n)
Friend Functions
Function that is allowed to access the private and protected members of a particular class from outside the class is called friend functions
Friend function of a class Not a member function Has direct access to private members
Just as member functions doUse keyword friend in front of
function declaration Specified IN class definition But they’re NOT member functions!
Friend Classes
Entire classes can be friends Similar to function being friend to class Example:
class F is friend of class C All class F member functions are friends of C NOT reciprocated Friendship granted, not taken
Syntax:
friend class F Goes inside class definition of "authorizing" class
Static Function
A function may be declared with the static keyword
Static functions live at class level, not at object level
Static functions may access static variables and methods, but not dynamic ones
Syntax:
public static int getNumSold(){return numTicketsSold;
}
class test{
private:static int n;public:static void show(){cout<<“n = “<<n;}
};int test::n = 10;void main(){
test::show();getch();
}
Output
n = 10
1. Write a program that creates three objects of a class student. Each object of the class must be assigned a unique roll number.
2. Compare OOP & structured programming
Assignment