Sie sind auf Seite 1von 9

Islamic University of Gaza

Faculty of Engineering

Computer Programming

Lab 10

C++ Classes & Object Oriented


Programming
By
Eng. Wafaa Audah

Dec. 2013

Object Oriented Programming


Object-oriented programming (OOP)
-

Encapsulates data (attributes) and functions (behavior) into packages called


classes. So, Classes are user-defined (programmer defined) types.

Data (data members) ------ Functions (member functions or methods)

In other words, they are Data + functions

Classes in C++
-

A class definition begins with the keyword class.


The body of the class is contained within a set of braces, { } ; (notice the semicolon).

Within the body, the keywords private: and public: specify the access level of the
members of the class << the default is private >>
public
can be accessed outside the class directly.
private
Accessible only to member functions of class.

Creating an object of a Class


Declaring a variable of a class type creates an object. You can have many variables of the
same type (class).
Instantiation
Once an object of a certain class is instantiated, a new memory location is created for it to
store its data members and code
You can instantiate many objects from a class type.

Circle c;

Implementing class methods


Class implementation: writing the code of class methods. There are two ways:
1. Member functions defined outside class
- Using Binary scope resolution operator (::)
- Ties member name to class name
- Uniquely identify functions of particular class
- Different classes can have member functions with same name
- Format for defining member functions
ReturnType ClassName::MemberFunctionName( ){

}
2. Member functions defined inside class

Accessing Class Members


Operators to access class members
- Dot member selection operator (.): Object ---- Reference to object
- Arrow member selection operator (->): Pointers

Memberwise Copy
In C++, you can also use the assignment operator = to copy the contents from one object
to the other. By default, each data field of one object is copied to its counterpart in the
other object. For example,
circle2 = circle1; copies the radius in circle1 to circle2. After the copy, circle1 and circle2
are still two different objects, but with the same radius.
Accessor and Mutator
-

A get function is referred to as a getter (or accessor), and a set function is referred
to as a setter (or mutator). A get function has the following signature:
returnType getPropertyName()

A set function has the following signature:


public void setPropertyName(dataType propertyValue)

Lab work #1
Build a class that handles time parts
1.

Create new project normally

2.

Create new file and save it as a header file:


Click right of the name of project_at the left of screen_ << new file << save
(ctrl+s) << put the namethe same of the class name << change the type
to header files (.h)

3.

Write the class code


class Time
{
private:
int hour, minute,second;
public:
Time();
Time(int h,int m,int s);
void printTime();
void setTime(int h,int m,int s);
int getHour(){return hour;}
int getMinute(){return minute;}
int getSecond(){return second;}
void setHour(int h){hour = h;}
void setMinute(int m){minute = m;}
void setSecond(int s){second = s;} };

4.
5.

Timer.h

Go to main file << Include header file (class): #include "Time.h".


Define member functions outside the class
Time::Time()
{ hour = minute = second = 0; }
Time::Time(int h,int m,int s)
{ hour = h;
minute = m;
second = s; }
void Time::setTime(int h,int m,int s)
{ hour = h;
minute = m;
second = s;}
void Time::printTime()
{ cout<<"The time is :("<<hour<<":"<<minute<<":"<<second<<")"
<<endl;}

6.

Write the code at the main which represent accessing Class Members

int main()
{
Time t1(3,55,54), t2;
t1.printTime();
cout<<"please enter time in hours, minutes and seconds: ";
int x,y,z;
cin>> x >> y >> z;
t2.setHour(x);
t2.setMinute(y);
t2.setSecond(z);
t2.printTime();
system("PAUSE");
return EXIT_SUCCESS;
}

Output

With My Best Wishes


....... Wafaa .

Goood Luck
9

Das könnte Ihnen auch gefallen