Advertise here.

Sunday 19 August 2012

C++ Programming Tutorial Lesson #10 - Classes


Creating a class

The class keyword is used when creating a class followed by the class name. All other parts of the class are put inside curly brackets. The last curly bracket must be followed by a semi-colon. Here is an example of how to create a class called Student.
class Student
{
};
A class can have private members which other parts of the program can't access and public members which can be accessed from outside the class. The private and public keywords are used for this.
class Student
{
private:
   char name[50];
public:
   int stunum;
};

Methods

Methods are functions that belong to an object. You must declare a method between the curly brackets of the class declaration and the full method with its code outside the class. Let's create methods for setting and getting the student number.
class Student
{
private:
   int stunum;
public:
   void setstunum(int sn);
   int getstunum();
};

void Student::setstunum(int sn)
{
   stunum = sn;
}

int Student::getstunum()
{
   return stunum;
}

Classes vs objects

An instance of a class called an object must be created before you can use it. This is because the class is like the plans for a house and the object is the actual house that has been built. Here is how you instantiate an object called stu of the Student class.
Student stu;

Constructors and destructors

A constructor is a method that runs when an object is instantiated. You can use a constructor to set the initial values of an object. A destructor runs when the object is destroyed. The constructor and destructor have the same names as the class and no return value but the destructor has a tilde in front of its name. Here is an example of the constructor and destructor for the Student class.
class Student
{
private:
   int stunum;
public:
   Student();
   ~Student();
};

Student::Student()
{
   cout << "Object has been created" << endl;
}

Student::~Student()
{
   cout << "Object has been destroyed" << endl;
}
A constructor can take parameters for when an object is instantiated and it can have default values for parameters. Here is an example of how to create a Student with the name John whose student number is 10000 and a Student called Mary who is given the default student number of 12345. It is a full program that includes methods for getting and setting all values.
#include<iostream>

class Student
{
private:
   char name[50];
   int stunum;
public:
   Student(char *n,int sn=12345);
   void setstunum(int sn);
   int getstunum();
   void Student::setname(char *n);
   char *Student::getname();
   ~Student();
};

Student::Student(char *n,int sn)
{
   strcpy(name,n);
   stunum = sn;
}

void Student::setstunum(int sn)
{
   stunum = sn;
}

int Student::getstunum()
{
   return stunum;
}

void Student::setname(char *n)
{
   strcpy(name,n);
}

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

Student::~Student()
{
   cout << "Object has been destroyed" << endl;
}

int main()
{
   Student stu1("John",10000);
   Student stu2("Mary");
   stu1.setstunum(50000);
   cout << stu1.getstunum() << endl;
   cout << stu1.getname() << endl;
   cout << stu2.getstunum() << endl;
   cout << stu2.getname() << endl;
   return 0;
}