Documente Academic
Documente Profesional
Documente Cultură
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.
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.
class Base{
public:
public Member;
private:
private Member;
protected:
protected Member;
}object1;
c. Inheritance
#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
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