Objects and Classes in Object Oriented Programming., Slides of Object Oriented Programming

Helpful in learning object oriented programming.Easy to understand.

Typology: Slides

2018/2019

Uploaded on 09/28/2019

urvah-javed
urvah-javed 🇵🇰

5

(1)

1 document

1 / 73

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object
Oriented
Programming
Chapter 06: Objects and Classes
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
https://sites.google.com/site/chishti
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49

Partial preview of the text

Download Objects and Classes in Object Oriented Programming. and more Slides Object Oriented Programming in PDF only on Docsity!

Object

Oriented

Programming

Chapter 06: Objects and Classes

Engr. Rashid Farid Chishti

[email protected]

https://sites.google.com/site/chishti

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 #include <stdlib.h> using namespace std; class mobile // class name should be the name of a concept { private: string company_name; string model_number; string emi_number; float cost; public: void set_info(string cn,string mn, string emi, float price) { // set data company_name = cn; model_number = mn; emi_number = emi; cost = price; } Using Mobile Phone as an Object (1/2)

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 #include <stdlib.h> using namespace std; class Height{ // A Height class private: int feet; float inches; public: void set(int ft, float in){ feet = ft; inches = in; } void get(){ // get height information from user cout << "\nEnter Your Height Information "<<endl; cout << "Enter feet: ";cin >> feet; cout << "Enter inches: "; cin >> inches; } void show(){ // display height information cout << "Height = "<< feet << " feet and " << inches << " inches."<<endl; } Using Class to Represent Height (1/2)

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 // A Constructor Example #include <stdlib.h> using namespace std; class House{ private: double area; // area in square feet public: House() : area(1000){ // a zero (no) argument constructor cout<< "House Created \n"; } void set_area(double a) { area = a; } double return_area() { return area; } void get_area(){ // get area info from user cout << "What is the area of House in Square Feet:"; cin >> area; } }; Constructors: House Example (1/2)