Demonstrating the use of objects and classes with constructors.
Posted by mark123
Last Updated: October 15, 2012
//Name Mark James
//2nd Year Computer Science Student
//Code demonstrating Object oriented Programming

#ifndef PersonH
#define PersonH
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
    string color;
    int age;
public:
    //Declaration of the Default Constructor that will deal with the overriding of the objects.c
    Person(string colorint,int ageint)
    {
        color=colorint;
    age=ageint;    
    }
//Call made to terminate object as soon as program terminates.

    Person()
    {
        cout<<"Object destructor called.."<<endl;
    }
    //This section deals with the accessors and Mutators that are needed for conversion to take place within the lifetime of the objects
    // created from this class.
    void setcolor(string colorset)
    {
        color=colorset;
    }
    string getcolor()
    {
            return color;
    }
    void setage(int agein)
    {
        age=agein;
    }    
    int getage()
    {
        return age;
    }

    void display()

    {
        cout<<"Color is"<<color<<endl;
        cout<<"Age is:"<<age<<endl;
    }
};
#endif
#include <cstdlib>
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
int main()
{
Person Mavis("Brown",25);//creation of an object called Mavis.
Mavis.display();
//Now we use mutators to change the values in Mavis.
Mavis.setcolor("White");
Mavis.setage(20);
//Now we use accessors to see the value
int ager;
ager=Mavis.getage();
{
 cout<<"Mavis age has now changed to"<<ager<<endl;
}
Mavis.display();
system("pause");

return 0;
}
Related Content