

































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Helpful in learning object oriented programming.Easy to understand.
Typology: Slides
1 / 73
This page cannot be seen from the preview
Don't miss anything!


































































International Islamic University H-10, Islamabad, Pakistan http://www.iiu.edu.pk International Islamic University H-10, Islamabad, Pakistan http://www.iiu.edu.pk (^) 1 Class Object
Introduction
Previously, programmers starting a project would sit down almost immediately and start writing code.
As programming projects became large and more complicated, it was found that this approach did not work very well. The problem was complexity.
Large programs are probably the most complicated entities ever created by humans.
Because of this complexity, programs are prone to error, and software errors can be expensive and even life threatening (in air traffic control, for example).
Three major innovations in programming have been devised to cope with the problem of complexity.
The Unified Modeling Language
The Unified Modeling Language (UML) is a graphical language consisting of many kinds of diagrams.
It helps program analysts figure out what a program should do, and helps programmers design and understand how a program works.
The UML is a powerful tool that can make programming easier and more effective.
We introduce each UML feature where it will help to clarify the OOP topic being discussed.
In this way you learn the UML painlessly at the same time the UML helps you to learn C++.
Class
A class serves as a plan, or template. It specifies what data and what functions will be included in objects of that class.
Defining the class doesn’t create any objects , just as the type int doesn’t create any variables.
A class is thus a description of a number of similar objects. functions data A class encapsulates attributes and functions
int main() { student Ali, Usman; // define two objects of class student Ali.set_age(21); // call member function to set age Usman.set_age(18); Ali.show_age(); // call member function to display age Usman.show_age(); system("PAUSE"); return 0; } A Simple Class (2/2)
An object has the same relationship to a class that a variable has to a data type.
An object is said to be an instance of a class, in the same way my Corolla is an instance of a vehicle.
In the program the class , whose name is student, is defined in the first part of the program.
In main(), we define two objects Ali and Usman that are instances of that class.
Each of the two objects is given a value, and each displays its value. Classes and Objects
Defining the Class
The definition starts with the keyword class, followed by the class name— student in this Example.
The body of the class is delimited by braces and terminated by a semicolon.
An object has the same relationship to a class that a variable has to a data type.
A key feature of object-oriented programming is data hiding. it means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside the class.
The primary mechanism for hiding data is to put it in a class and make it private.
Defining the Class
Private data or functions can only be accessed from within the class.
Public data or functions, on the other hand, are accessible from outside the class.
The data member age follows the keyword private , so it can be accessed from within the class , but not from outside.
Member functions (also called methods or messages ) are functions that are included within a class.
There are two member functions in student class: set_age() and show_age().
Defining the Class
Remember that the definition of the class student does not create any objects. It only describes how they will look when they are created.
Defining objects means creating them. This is also called instantiating them, because an instance of the class is created. An object is an instance of a class.
ali.set_age(21); This syntax is used to call a member function that is associated with a specific object ali.
Member functions of a class can be accessed only by an object of that class.
// mobile.cpp demonstrates mobile phone as an object #include
If you were designing an inventory program you might actually want to create a class something like mobile.
It’s an example of a C++ object representing a physical object in the real world—a mobile phone.
Standard C++ includes a new class called string.
Using string object, you no longer need to worry about creating an array of the right size to hold string variables.
The string class assumes all the responsibility for memory management. Using Mobile Phone as an Object
#include
Constructors
We see that member functions can be used to give values to the data items in an object.
Sometimes, however, it’s convenient if an object can initialize itself when it’s first created, without requiring a separate call to a member function.
Automatic initialization is carried out using a special member function called a constructor.
A constructor is a member function that is executed automatically whenever an object is created.
Constructor has the same name of class and it has no return type.
#include