Oleh: leonard260788 | Oktober 28, 2008

Object Oriented Programming

First of all, you must think about Object. People, car, pencil, etc are objects in the real world. We can carry these concept into Object Oriented Programming(OOP). Each object can have properties and methods. People have name and age properties for example. People can speak, eat, walk, kick, etc.

To make object in Object Oriented Programming, we must define class. Class is a blueprint of an object. In that class , we define the properties and the methods.
For example, I want to define People class. The People class will have name and age properties and also speak method. I will use C++ as the programming language. So, the declaration of the class will look like this :

class People{
char name[21];
int age;

speak();
}

Moreover, each class will have a method that will be executed, each time the object is made. This method is called constructor. The constructor must have the same name with the class name. We make People class , so the constructor name must be “People”. So, We add the constructor to the class.

class People{
char name[21];
int age;

People(); // constructor

speak();
}

Beside constructor, the class will also have destructor. Destructor must be named “~” + class name . So, the People destructor will look like this “~People”.
Both of constructor and destructor can not have return type.

class People{
char name[21];
int age;

People(); // constructor
~People(); // destructor

speak();
}

You have to keep the data private and the method public to make information hidding. The data can not be accesed outside the class. The data can only be accessed by the public method. Making setter and getter function is the way to do that. So , we add setter and getter method to People class.

class People{
private : // make the data private
char name[21];
int age;

public : // make the method public

People(); //constructor with no parameter
People(char[], int); //constructor with two parameters
~People(); //destructor
void setname(char[]); //setter function
void setage(int); //setter function
char* getname(); //getter function
int getage();  //getter function
void speak();  //member function
};

Now we make the methods definition and we write the main method to test the class.
Here is the full source code :

#include<constrea.h>
#include<string.h>

class People{
private :
char name[21];
int age;

public :
People(); //constructor with no parameter
People(char[], int); //constructor with two parameters
~People(); //destructor
void setname(char[]); //setter function
void setage(int); //setter function
char* getname(); //getter function
int getage();  //getter function
void speak();  //member function
};

//constructor with no parameter
People::People(){
setname(“noname”);
setage(0);
}

//constructor with two parameters
People::People(char name[], int age){
setname(name);
setage(age);
}

People::~People(){
cout << “destructor” << endl;
}

void People::setname(char name[]){
strcpy(this->name, name);
}

void People::setage(int age){
this->age = age;
}

char* People::getname(){
return name;
}

int People::getage(){
return age;
}

void People::speak(){
cout << “My name is ” << name << endl;
cout << “I’am ” << age << ” years old” << endl;
}

void main(){

People john; // calling constructor with no paramater
/*
make an object of People class
the object name is john
*/
john.speak(); //calling speak method

People susi(“susi”, 20); // calling constructor with two parameters

susi.speak();

/*
setting name = john and age = 30 to john
*/
john.setname(“john”);
john.setage(30);
john.speak(); // john speak “My name john I’m 30 years old”

getch();

}

The pattern of calling the method is ObjectName.MethodName(Parameter);. But ,if you want to call the method of the class, you must first making an object of that class. Simply by writing People john; You have already made an object. The name of the object is john and john is an object of People class. Now, you can use the method of People class.

In the source, you will see that the constructor is overloaded. Yes, we can overload the constructor. In this example, I make two constructors. The first constructor have no parameter and the second one have two parameters. So , if you want to call the first constructor, you can write People john; . This will execute constructor with no parameter.
If you want to call the second constructor, you can write People susi(“susi”, 20); . It will call constructor which has two parameters. But how can I call the destructor? Good question. The destructor will be executed automatically when the object is dead.


Beri tanggapan

Your response:

Kategori