Sunteți pe pagina 1din 5

Computer Laboratory - JTE Unand Chapter V

CHAPTER V
OBJECT-ORIENTED PROGRAMMING

I. Objectives

Practitioner can understand the difference of class type public, private and
protected and how to use it.
Practitioner can understand what is constructor and destructor.
Practitioner can practicing the concept of using class in a computer programming.
Practitioner can understand what inheritance is and how to use it.

II. Theory

a. Classes
Classes are an expanded concept of data structures. Just like a data structures,
they can contain data members, but they can also contain functions as members.
Class in c++ introduced recently as a new theories that support object-oriented
program. A class can organize data and function in the same structure. It declared
using keyword class, which has the same purpose with the keyword struct just like
the structure in a chapter before but class has an additional possibility to include the
function as a member in not only as a data.

Syntax of the class:


class Class_name{
permission_label_1:
member1;
permission_label_2:
member2;
...
} object_name;

Where the class_name is a name of a class that defined by user and field
optional is an object_name, or a legitimate object identifier. The body of the
declaration can contain a members, where it can be a data declaration or a function,
and a permission_labelwhere it can be one of these keyword: private:, public:, or
protected:. These permission label making a decision so that a member will have
the right to access, accessed or restricted just as what the permission label is made
there.
1. A permission label private is a permission label where the members of a class can
only be accessed in its own class or from the friend class.
2. A permission label protected is a permission label where the members of a class
can only be accessed in its own class or from the friend class and the member of
the class from the derived class.
Computer Laboratory - JTE Unand Chapter V

3. A permission label public is a permission label where the members of a class can
be accessed from any class or main program and other function where the class can
be called.

The structure of a class

class Base{
public:
public Member;
private:
private Member;
protected:
protected Member;
}object1;

b. Constructors and Destructors

Constructor used to initialize a variable or dynamic memory when an object


made. In a class, a function of constructor using the same name as its class name.
A constructor will automatically created when a new instance object of the class
made.
Destructor will automatically called when an object released from the
memory, or its no longer be used (For example if we defined an object as a local
variable in a function and that function itself will be over) or released with a delete
operator. Destructor have the same name as a class but it start with a tilde (~) and
destructor not return the value.
Example:
class Mobil
{
private :
introda, pintu;
charwarna[10];
public :
Mobil(); //Konstruktor
~Mobil(); //Destruktor
void info();
}object1;

c. Inheritance

Inheritance is a subclass of the main class. inheritance is when an object or


class is based on another object or class, using the same implementation (inheriting
from a class) or specifying implementation to maintain the same behavior (realizing
an interface; inheriting behavior). It is a mechanism for code reuse and to allow
independent extensions of the original software via public classes and interfaces.
The relationships of objects or classes through inheritance give rise to a hierarchy.
A subclass can also be a superclass for another class and it called multilevel
inheritance.
Example:
#include <iostream>
Computer Laboratory - JTE Unand Chapter V

#include <string>
using namespace std;
class Mother{
public:
void say(string a) {
cout<< " ' " << a << " ' " <<endl;
}
};
class Child : public Mother{
};

int main() {
Child Daughter;
Daughter.say("Pemrograman Komputer");
system("PAUSE");
return 0;
}
Computer Laboratory - JTE Unand Chapter V

III. Procedure of Experiment

3.1 Class ,Constructor and destructor


1 #include<iostream>
2
3 using namespace std;
4
5 class Rectangle{
6 public:
7 int *length, *width;
8
9 public :
10 Rectangle (int a, int b){
11 length = new int;
12 width = new int;
13
14 *length = a;
15 *width = b;
16 }
17 int arectangle(){
18 return (*length * *width);
19 }
~Rectangle (){cout <<"\nThe result before destructor executed is :
20
"<<(*length * *width)<<endl;
21 cout <<"The address of length is : "<<&length << endl;
22 cout <<"The address of width is : "<<&width<< endl<<endl;
23 system("pause");
24 delete length;
25 delete width;
cout <<"\nThe result after destructor executed is : "<<(*length *
26
*width)<<endl;
27 cout <<"The address of length is : "<<&length << endl;
28 cout <<"The address of width is : "<<&width << endl<<endl;
29 system("pause");
30 }
31 };
32
33 int main(){
34 Rectangle square1 (3,4);
cout <<"The rectangle of the square1 is : "<< square1.arectangle() <<
35
endl<<endl;
36 }
Computer Laboratory - JTE Unand Chapter V

3.2 Inheritance
1 #include <iostream>
2 using namespace std;
3 class basic
4 {
5 public :
6 int X;
7 public :
8 basic ()
9 {X=10;}
10 };
11
12 class inherit : public basic
13 {
14 int Y, result;
15 public :
16 void setY (int YY)
17 {Y=YY;}
18 void timesXY ()
19 {result=X*Y;}
20 int get_result()
21 {return result;}
22 };
23
24 int main ()
25 {
26 basic A;
27 inherit B;
28 cout<<" x : " <<endl;
29 cin>>A.X;
30 B.setY(5);
31 B.timesXY();
32 cout<<"The result of X times Y is : "<<B.get_result()<<endl;
33 system("PAUSE");
34 return 0;
35 }

IV. Problems

1. What is the difference between struct and class ?


2. Make a simple program about class! dont take from modul!
3. Explain about the meaning of constructor and destructor!
4. Explain about the inheritance and make a simple program about inheritance! dont
take from modul!
5. Explain the difference of public, private and protected class!
6. Can inheritance used in public, private and protected class? explain each of them!
7. Make syntax for class, destructor and constructor
8. What is the function of delete in procedure 1?
9. What is the function of ~ (tilde) on program in procedure 1?
10. Can we use more than one object on a Class? Make a simple program about it!

S-ar putea să vă placă și