You are on page 1of 31

Data Structures and Object Oriented

Programming
EC-204

Instructor
Ms. Kanwal Naveed

Department of Mechatronics Engineering


College of E&ME
National University of Sciences and technology
Lecture Details
Object Oriented Approach
Objects
Classes
Private and Public data
Member Functions
Features of Object Oriented Approach
Encapsulation
Inheritance
Reusability Reading:
Overloading Ch 1 of Textbook
Ch 6 of Textbook
Object Oriented Programming
The fundamental idea is to Class class_name
combine into a single unit both {
data and functions. Access specifier
The definition of this unit is Variable Declaration;
called a Class Function Declaration;
}
This unit when called in main is
called an Object
Int main ()
An objects functions are called {
member functions in C++ class_name obj1;
And its data are called data }
members
Human is a class and Ali is an
object
Objects and Classes
Objects belong to classes
A class can have multiple objects.
A class serves as a plan, or a template, or
sketch.
It merely specifies what data and what
functions will be included in the objects of that
class.
A class consists of data items and member
functions. An instance of this class is known
as an object.
Objects and Classes
Declaring a class doesnt create any objects,
just as mere existence of data type int doesnt
create any variables.
Remember Functions??
A class is thus a description of a no. of similar
objects.
For instance, HUMAN is a class, and JOHN is
its instance (object)
Objects and Classes
Declaring a class doesnt create any objects,
just as mere existence of data type int doesnt
create any variables.
A class is thus a description of a no. of similar
objects.
For instance, HUMAN is a class, and JOHN
and SARAH are its instances (objects)
Classes and Objects:
Example
Learning the example in two steps:
1. Learn how to write a class
2. Learn how to define its objects
Example: Step 1
class shape
{private:
int length;
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
Example: Step 1
class shape
{private:
keyword int length;
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
Example: Step 1
class shape Class name
{private:
keyword int length;
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
Example: Step 1
class shape Class name
{private:
keyword int length;
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
semicolon
Example: Step 1
class shape Class name
{private:
keyword int length; Access Specifiers
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
semicolon
Example: Step 1
class shape
visible to class members and
{private:
member functions only!
int length;
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
Why Private: Data Hiding
An important characteristic of OOP
Keeps data hidden inside the class to keep it safe
from functions outside the class
Hides data from part of the program that dont
need to access it
Only member functions can access it
Access Specifier
Private
Can only be accessed from within the class
Public
Can be accessed from outside the class
Generally,
Data is kept private
Member functions are kept public
But functions can also be made private
Example: Step 1
class shape
{private:
int length;
public: visible to the entire program!
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
Example: Step 1
class shape
{private:
int length; class data
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
Example: Step 1
class shape
{private:
int length;
member function to set data
public:
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
Example: Step 1
class shape Class name
{private: Hidden data
keyword int length;
public: Public functions
void setdata(int l)
{length = l;
}
void showdata()
{cout << "Data is " << length << endl;
}
};
semicolon
Example: Step 1
class shape Class name
{private: Hidden data
keyword int length;
public: Public functions Set hidden variable
void setdata(int l) length equal to
{length = l; external variable l

}
void showdata()
{cout << "Data is " << length << endl;
}
};
semicolon
Example: Step 1
class shape Class name
{private: Hidden data
keyword int length;
public: Public functions Set hidden variable
void setdata(int l) length equal to
{length = l; external variable l

}
void showdata()
{cout << "Data is " << length << endl;
}
Display value of hidden
}; variable length
semicolon
Objects
To use a class in a C++ program, its objects
must be instantiated
Similar to declaring a variable
name of the object
int main()
{shape line; An object of the class
shape

name of the class Different from simple


line.setdata(5); function call
line.showdata();
}
int main()
{shape line1,line2;

line1.setdata(5);
line1.showdata();
line2.setdata(4);
line2.showdata();
}
Example 2
Write a class called Area with data member as
length and width. Write three member
functions
getdata: get the value of length and width from the
user
showdata: displays the area of the shape
Declare two objects called square and
rectangle and display their areas.
class Area
{private:
int length, width, area;
public:
void getdata()
{cout << Enter length; cin >> length;
cout << Enter width; cin >> width;

}
void showarea()
{cout << Area is " << length*width;
}
};
int main()
{
Area Square, Rectangle;
Square.getdata();
Rectangle.getdata();
Square.showarea();
Rectangle.showarea();
}
Four Important Properties
Encapsulation
Inheritance
Re-usability
Polymorphism and overloading
Encapsulation
Member functions in C++, typically provide the only way to
access the data items in a class.
To read a data item in an object, its member function in the
object is called. It accesses the data and returns the value.
The data cannot be accessed directly. Thus data and its
function are said to be Encapsulated into a single entity.
Encapsulation leads to Data Hiding
The data is hidden to keep it safe from accidental alteration.
This techniques is known as Data Hiding.
To modify the data in an object, you need to access the
member functions in the object thus simplifying writing,
debugging, and maintaining the program.
Inheritance
The idea of classes and subclasses.
Derive other classes from an existing class
Each class shares common characteristics with
the class from which it was derived, and can
also add its own modifications, additions.
For instance, VEHICLE is a class from which
CAR, TRUCK, BUS, MOTORCYCLE classes
can be derived.
Inheritance
The original class is called the BASE CLASS;
the others are DERIVED CLASSES
Re-usability
Language extensibility (create new data types)
Reusability
Once a class has been written, created, and
debugged, it can be distributed to other
programmers for use in their own programs.
This is called reusability.
Inheritance: A programmer can take an
existing class and, without modifying it, add
additional features and capabilities to it. This is
done by deriving a new class from the existing
one.
Polymorphism and Overloading
Using operators or functions in different ways
depending on what they are operating on is
called polymorphism.
4 + 5 <-- integer addition
3.14 + 2.0 <-- floating point addition
s1 + "bar" <-- string concatenation!

When an existing operator, such


as + or =, is given the capability to operate on
a new data type, it is said to be overloaded.

You might also like