3 Class Jan16 589d5c54230db
3 Class Jan16 589d5c54230db
&
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
• 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.
class
Class are structures that contain not only data declarations but also
function declarations.
public:
void SetValue (float);
float GetValue ( );
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 ];
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
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.
•In C++ classes can contain both data members and member
functions
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.
res1 = c1.series(1000,1000);
res2 = c1.parallel(1000,1000);
To identify them as being from the member function c1 (of type circuit) we
prefix the function name with the class name c1.
For example, we could have a different object called occam and call our
member functions thus
occam.series();
occam.parallel();