Object Oriented Programming With C Constructors Getter Setter

- - Uncategorized

Object Oriented Programming C++

Here through this article, we will discuss about the basics of Object Oriented Programming. Our codes will be based on C++ programming language while the concept is the same for other OOP languages too. We will write 3 files amongst which one is the header file, the second one is the implementation of the header template. Finally we will have one main program. By the end of this read, you will be able to write codes in Object Oriented Programming languages. We will cover constructor, destructor, setter and getters.

Class definition file Computer.h

 

#include
using namespace std;

class Computer{
    private:
        string deviceType;
        string nameofBrand;
    public:
        Computer(string brandName="lenovo",string typeofDevice="laptop");
        ~Computer();
        void setBrandName(string brandName);
        void setDeviceType(string typeofDevice);
        string getBrandName();
        string getDeviceType();
        void displayDeviceInfo();
};

The above program shows the structure of our class Computer. The file Computer.h is our class template file.

1. Line 1 and 2 are the include statements of our input/output header file i.e iostream

2. Line 4: Our class for this example is Computer in which the starting alphabet is capital which is the convention of OOP.

3. Line 5 to 7: In C++ we place the private variables after the keyword private followed by a colon. For our example we have two private variables deviceType and nameofBrand. The private variables cannot be accessed by the object.variableName while it is possible to access it via member functions i.e the methods that are public. Basically on a general sense, private variables can be accessed only within the class.

4. Line 8 to 15 are the member functions of class Computer. Here the functions/methods are placed after the keyword public: . This means the object of class Computer can access these member functions directly via object.memberFunction().

5. Line 9 and 10 are different than the other member functions. Line 9 is the definition of the constructor for our class Computer. The constructor contains the name same to the class name. This is the convention for all the OOP. A constructor has no return type as it is basically used for the initialization of the private variables. The code inside the constructor runs at the time of object creation. In our header file, we have two parameters in the constructor and each of the parameter is initialized by the default value. Line 10 is the definition of destructor. In C++ destructor has same name as the class name except it contains “~” sign before the name. Destructor are basically used to destroy other classes initialized in the current class.

6. Line 11 to 14 are the setter and getter methods for the private variables deviceType and nameofBrand. The setter methods have no return type and takes values through parameters which are to be set to the private variables. The getter methods are used to access the private variables and takes no parameter as it’s function is to return the value and not accept any parameters. Therefore getters have return type which is based on the type of private variables.

7. Line 15 is the member function like all others which has return type void and takes no parameter/argument.

The following file is Computer.cpp file which contains the implementation of the class definition Computer.h

Class implementation file Computer.cpp

#include
#include "Computer.h"
using namespace std;

Computer::Computer(string brandName,string typeofDevice){
    setBrandName(brandName);
    setDeviceType(typeofDevice);
}

Computer::~Computer(){
    cout<<"Object Destroyed!!"<<endl;
}

void Computer::setBrandName(string brandName){
    nameofBrand = brandName;
}

void Computer::setDeviceType(string typeofDevice){
    deviceType = typeofDevice;
}

string Computer::getBrandName(){
    return nameofBrand;
}

string Computer::getDeviceType(){
    return deviceType;
}

void Computer::displayDeviceInfo(){
    cout<< "It is a   " << getDeviceType() << "and belongs to  "<< getBrandName()<<endl;
}

1. Line 1 to 3 contains the include statements. We have to include the header file Computer.h in our implementation file. The standard header files are included via statement #include<header> while the header files created by the user are included via statement #include “Header.h”

2. Line 5 to 8 is the implementation of the constructor of the class Computer. It takes two arguments namely brandName and typeofDevice. Inside the function setBrandName and setDeviceType methods are called with the parameters brandName and typeofDevice respectively. Whenever an object of class Computer is created, the codes inside the constructor is run immediately.

3. Line 10 to 12 is the implementation of the Destructor of the class Computer. The destructor is basically used to terminate/kill the objects of the other classes initialized in the current class. In our example, we have done nothing but printed that the object has been destroyed.

4. Line 14 to 16 is the implementation of the method setBrandName. It is a setter method. Conventionally setter method begins with “set” followed by the variable name. Our setBrandName takes one argument and is of return type void. Inside the method, nameofBrand is set to the value passed in as an argument. nameofBrand is our private variable hence a public method is used to access and alter it’s value i.e setBrandName.

5. Line 18 to 20 is the implementation of the setDeviceType. Similar to the setBrandName method, it is also a setter method. This method is used to set the value of the private variable typeofDevice. This method also takes one argument and is of return type void.

6. Line 22 to 24 is the implementation of the method getBrandName. Unlike setBrandName, getBrandName is a getter method that is used to return the value of a private variable which in this case is nameofBrand. The return type of a getter method is same as the type of variable it returns. In our example, getBrandName is of string return type which takes no parameter/argument.

7. Line 26 to 28 is also a getter method that is used to return the value of the variable deviceType. It is of string return type because it is used to access the value of the variable deviceType which is of type string.

8. Finally we have our last method in the class computer which in this case, we are using to print out the information of the device based on the entries entered at the type of object creation. Method displayDeviceInfo is a void return type method that takes no parameter. Here we are using the standard of method of accessing the private variables i.e using getter methods. The method when invoked on an object prints the deviceType and nameofBrand.

Let us take a look at our main program where we create objects of class Computer and invoke various methods of the class. Below is the main program.

Main program testprogram.cpp

#include
#include "Computer.h"
using namespace std;

int main(){
    string deviceBrand;
    string typeofDevice;

    Computer computers[5];

    for(int i = 0; i < 5; i++){


        cout<< "Enter the brand of your computer for position "<< i+1<<endl;
        getline(cin,deviceBrand);

        cout<< "Enter the type of computer for position "<< i+1<<endl;
        getline(cin,typeofDevice);

        Computer objectHolder(deviceBrand, typeofDevice);

        computers[i] = objectHolder;
    }

    for(int i = 0; i < 5; i++){

        Computer objectHolder = computers[i];
        objectHolder.displayDeviceInfo();
        //computers[i].displayDeviceInfo();
    }
}

1. Line 1 to 3 are the statements to include the iostream and our Computer class that we coded earlier. As discussed earlier, we include the non-standard class (Computer.h in this case) in the format #include “Header.h”. One thing to note is that we include the class definition file and not the implementation file.

2. Line 6 to 7, we declare two variables of type string.

3. Line 9 begins the OOP portion. Here we are declaring an array of type Computer of size 5. This means each index of the array computers can hold an object of Computer class.

4. Line 11 to 23 is a for loop where we iterate for the number of times equal to the size of our array I.e five. We then take input from the user for the variables deviceBrand and typeofDevice declared earlier. Next, we create an object named objectHolder of class Computer. You will notice we have passed in two arguments at the time of creation of the object. Now this invokes the constructor of Computer class. Everything that’s inside of the constructor gets run at this instance. Finally, we are assigning the objectHolder to the array’s current index. Summing up we will have five objects assigned to the array at the end of our loop.

5. Line 25 to 30 is another loop. Here we invoke the displayDeviceInfo method of the class Computer on each object stored in the array computers. On invoking the method, we get the information of the device we’ve entered at the time of creation of the object.

Following is the output of our program. You will see Object destroyed being printed several time. This is because we have a destructor method in our computer class.

Post Tags:

bhishan

I am Bhishan Bhandari, a CS student and life hacker. I specialize in automation. I sell my services on fiverr. You can hire me for projects here Buy Services Follow me on github for code updates Github You can always communicate your thoughts/wishes/questions to me at bbhishan@gmail.com