0% found this document useful (0 votes)
31 views26 pages

3 Class Jan16 589d5c54230db

The document discusses different programming approaches including procedure oriented programming (POP) and object oriented programming (OOP). POP emphasizes the use of functions and subroutines with no data hiding, while OOP emphasizes data hiding, code reusability through inheritance, and software reusability to increase programmer productivity. OOP is more efficient for developing complex programs through features like encapsulation of data and code into classes and objects.

Uploaded by

Samuel Gabriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views26 pages

3 Class Jan16 589d5c54230db

The document discusses different programming approaches including procedure oriented programming (POP) and object oriented programming (OOP). POP emphasizes the use of functions and subroutines with no data hiding, while OOP emphasizes data hiding, code reusability through inheritance, and software reusability to increase programmer productivity. OOP is more efficient for developing complex programs through features like encapsulation of data and code into classes and objects.

Uploaded by

Samuel Gabriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Structures

&
classes 2
Procedure Oriented Programming
Object Oriented Programming (I)
• There are many approaches to write program
 Procedure Oriented Programming (POP)
 Object Oriented Programming (OOP)
 Top-Down Programming
 Bottom-Up Programming

• Aim of all these approaches are to make the programming efficient


(develop complex program, easy to fix bug, easy to understand and
modify)

• POP emphasis the use of functions and subroutines. Functions are called
repeatedly in a program to execute tasks. No data hiding in POP approach.
Procedure Oriented Programming
Object Oriented Programming (II)
• POP emphasis the use of functions and subroutines. Functions are called
repeatedly in a program to execute tasks. No data hiding in POP approach.
Procedure Oriented Programming
Object Oriented Programming (III)
• To develop a more complex program, it would be more efficient using
OOP method.
• Some features in OOP
- Data hiding: programmer can hide the important core data from
external world. Variables and functions can be pack together in a class.
- Code reusability: code can be reuse using inheritance.
Object Oriented Programming
What is OOP(object oriented programming)? It is just a way to
properly organize your code into logical components that are
easier to program.

OOP emphasizing software reusability in program coding. It


increase the programmer productivity and reduce the cost of the
software maintenance.
Classes
structure
collection of related data items stored in one place and referenced
under one name

class
Class are structures that contain not only data declarations but also
function declarations.

struct student class student


{ { char id[5];
char id[5]; char name[25];
char name[25]; char gender;
char gender; int age;
int age;
void dataInput( );
}; void display_data( );
};
Class
name Class declaration
class Employee method
{
float salary;

public:
void SetValue (float);
float GetValue ( );

}; //don’t forget the semi-colon

• Keyword class introduces a class declaration


• class name  Employee
• member of the class should fall into { };

• class Employee has one data member.


Can you name the member variable of the class?

• Can you list the member functions of the class?


Member Function
class Employee
{
float salary;
Private
public:
void SetValue (float value)
{
salary = value;
}
Public
float GetValue ( )
{
return salary;
}

Private : can only be accessed inside the class


Public : accessible by anyone
Class object
class Employee
{
float salary;

public:
void SetValue (float value) void main( )
{ {
salary = value + 500;
} float pay;
Employee Intel;
float GetValue ( )
{ Intel.SetValue (2500.00);
return salary; pay = Intel.GetValue( );
} cout<<“\nYour pay $ ” << pay;

} }

* Intel is an object, it does not just hold data, it also has functions that act on the data.
Class Object
You can also declare an array of objects
Employee Intel [ 5 ];

Once an object is declared, it can be


manipulated by any of the functions
declared within the object’s class.

void main( ) User cannot directly access the data items


{ contained within an object.
Salary = 2500 + 500;
float pay;
Employee Intel; They can only be accessed by the
functions defined within the class.
Intel.SetValue (2500.00); Intel.SetValue (2500.00);
pay = Intel.GetValue( );
cout<<“\nYour pay $ ” << pay; This property of encapsulation has the
effect of hiding the implementation details
} from the user. (information hiding)
Example 1 (Class)
Class circuit
{
private:
float rtemp; // rtemp is private to the class

public: // the two member functions are public


float parallel(float r1, float r2)
{
return( (r1*r2) / (r1 + r2) );
}
float series(float r1, float r2)
{
return(r1+r2);
}

}; // note the semicolon at the end of the class definition.


Example 1 (Class)
#include <iostream.h> int main(void)
{
class circuit circuit c1; // an object called c1 declared
{ float res1, res2;
private:
float rtemp; res1=c1.parallel(2000,1000);
cout << “Parallel resistance is “ << res1 ;
public: cout << endl;
float parallel(float r1, float r2)
{ res2=c1.series(2000,1000);
return( (r1*r2)/(r1+r2) ); cout << “Series resistance is “<< res2;
} cout << endl;
float series(float r1, float r2)
{ return(0);
return(r1+r2);
} } // end of main program
}; // end of class
Question 1
Develop a C++ class for adding and subtracting numbers.

The class (called add_sub) should contain two member functions


add() and sub() that accept two float variables ‘a’ and ‘b’ and
return the result.
Objects and Classes
class student int main( )
{ {
char id[5]; student Tan, Abdel;
char name[25];
char gender; Tan.dataInput( );
int age; Tan.display_data( );

public:
void dataInput( ); return 0;
void display_data( );
}
};
object
Member function class
run time entity
class define data & operation
in object Object Orientated Programming
class Vs struct
class default as private
struct default as public

class
struct
Objects and Classes
Employee

Full time Part time

PJ Penang PJ Penang

• divide your program into different classes that contain related objects

• The classes of related objects contain all the data and functions that use
that data.
Question 2
Write a program to calculate the distance between two
points using the following formula (Figure 1). Program
should prompt the user to enter the coordinates of the line.

Figure 1: Distance between the points


Question 3
Research several car-pooling websites. Create an
application that calculates your daily driving cost, so that you
can estimate how much money could be saved by car
pooling, which also has other advantages such as reducing
carbon emissions and reducing traffic congestion.
Extra notes
Objects and Classes:
An Object is any runtime entitiy (I.e., could be a routine to
calculate averages).

A Class is a type that defines the data contained in an object,


along with the operations that are to be performed by the object.

The operations to be performed by the object are defined within


functions that are contained within the object.

C++ functions that are part of an object are sometimes called


member functions.
An example of an Object:

Class average The object is the entity to


{ calculate averages.
private:
float temp; The data of the object is temp,
(a pointer) data, and ave.
public:
float *data, ave; The operations to be performed
by the object are defined within
float calc_averages(float *data); the function calc_averages()

} The class defines the object type


called average.
To create an object: average MyAverage

MyAverage is an object of the class average.


Classes in C++
•Classes are a general form of structure

•In C a struct gathers together data members

•In C++ classes can contain both data members and member
functions

•C++ contains both struct and class.

The main difference between class and struct is that in a struct all members of the
struct are public, while

in the class the members are private (by default) - but can be made public.
Struct and class
•In the struct variables that make up the structure are all public.

•In a class members are made public with the public keyword
and private with the private keyword.

•In some parts of the class members can be made public, while
in others, members can be made private.

•The class can contain member functions. These are usually


declared public.

•However, member functions can also be declared private (as we


shall see in a forthcoming example - later).
Using the class
Once we have declared a class type we can declare an instance
of the class as

circuit c1; // this declares an instance of the class called c1


// c1 is an object of the class circuit.
Now we can use the class object thus

float res1, res2;

res1 = c1.series(1000,1000);
res2 = c1.parallel(1000,1000);

note the prefix c1 referring to the class c1 and member functions


More on the class
res1 = c1.series(1000,1000);
res2 = c1.parallel(1000,1000);
In the above the two member functions series() and parallel() are called.

To identify them as being from the member function c1 (of type circuit) we
prefix the function name with the class name c1.

It is possible to have other functions series() and parallel() which belong to


other classes, perhaps performing a different task to those of c1.

For example, we could have a different object called occam and call our
member functions thus

occam.series();
occam.parallel();

You might also like