C++ PROGRAMMING EBOOK LESSON 10 – CLASSES

The collection keyword is utilised when creating a collection followed by the collection name. All another parts of the collection are place exclusive frizzy brackets. The terminal frizzy redact staleness be followed by a semi-colon. Here is an warning of how to create a collection titled Student.

collection Student
{
};

A collection crapper hit clannish members which another parts of the information can’t admittance and open members which crapper be accessed from right the class. The clannish and open keywords are utilised for this.

collection Student
{
private:
char name[50];
public:
int stunum;
};

Methods

Methods are functions that belong to an object. You staleness tell a method between the frizzy brackets of the collection papers and the flooded method with its cipher right the class. Let’s create methods for environment and effort the enrollee number.

collection 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 happening of a collection titled an goal staleness be created before you crapper ingest it. This is because the collection is aforementioned the plans for a concern and the goal is the actualised concern that has been built. Here is how you instantiate an goal titled stu of the Student class.

Student stu;

Constructors and destructors

A creator is a method that runs when an goal is instantiated. You crapper ingest a creator to ordered the initial values of an object. A destructor runs when the goal is destroyed. The creator and destructor hit the aforementioned obloquy as the collection and no convey continuance but the destructor has a tilde in face of its name. Here is an warning of the creator and destructor for the Student class.

collection Student
{
private:
int stunum;
public:
Student();
~Student();
};

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

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

A creator crapper verify parameters for when an goal is instantiated and it crapper hit choice values for parameters. Here is an warning of how to create a Student with the study Evangelist whose enrollee sort is 10000 and a Student titled Jewess who is presented the choice enrollee sort of 12345. It is a flooded information that includes methods for effort and environment every 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;
}

Comments are closed.