object oriented programming using c++

32
Using C++ Object Oriented Programming

Upload: muhammad-azam

Post on 13-Dec-2014

787 views

Category:

Education


2 download

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 & functions

TRANSCRIPT

Page 1: Object Oriented Programming Using C++

Using C++

Object Oriented Programming

Page 2: Object Oriented Programming Using C++

Contents

Features of OOPClassesObjects & Creating the ObjectsConstructors & Destructors Friend Functions & ClassesStatic data members & functions

Page 3: Object Oriented Programming Using C++

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

Page 4: Object Oriented Programming Using C++

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.

Page 5: Object Oriented Programming Using C++

Effects of OO methodology on software design

Maintenance

Extensibility

Reusability

Page 6: Object Oriented Programming Using C++

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

Page 7: Object Oriented Programming Using C++

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

Page 8: Object Oriented Programming Using C++

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.

Page 9: Object Oriented Programming Using C++

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

Page 10: Object Oriented Programming Using C++

Functions of an Object

Tasks or actions performed by the object are

known as functions or methods.

Page 11: Object Oriented Programming Using C++

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

Page 12: Object Oriented Programming Using C++

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

Page 13: Object Oriented Programming Using C++

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.

Page 14: Object Oriented Programming Using C++

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.

Page 15: Object Oriented Programming Using C++

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.

Page 16: Object Oriented Programming Using C++

Write a program that declares a class with a data member

and two member functions

OUTPUT:

enter number 10the value of n= 10

Page 17: Object Oriented Programming Using C++

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

Page 18: Object Oriented Programming Using C++

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

Page 19: Object Oriented Programming Using C++

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

Page 20: Object Oriented Programming Using C++

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

Page 21: Object Oriented Programming Using C++

Output

the constructor of first= num = 0ch = xthe contents of second = num = 100ch = p

Page 22: Object Oriented Programming Using C++

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

Page 23: Object Oriented Programming Using C++

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}

Page 24: Object Oriented Programming Using C++

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.

Page 25: Object Oriented Programming Using C++

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

Page 26: Object Oriented Programming Using C++
Page 27: Object Oriented Programming Using C++

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)

Page 28: Object Oriented Programming Using C++

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!

Page 29: Object Oriented Programming Using C++

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

Page 30: Object Oriented Programming Using C++

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;

}

Page 31: Object Oriented Programming Using C++

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

Page 32: Object Oriented Programming Using C++

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