C++ tutorial-C++ class and object


C++ Classes and Objects

In an Object-Oriented Programming(OOP) concept, Class and Object are the core. Class is a template that can be used to create an object. Therefore, object is the instance of the class.

In this page you will learn how to create classes and objects, understand the class members and know how to define them, understand the users of private and public keywords.
Creating a class

To create a class, you must use class keyword followed by the name of the class.

Example:

class Student
{


};

A class can have three types of members-private, public, and protected members. The private member can’t be accessed by the code outside the class. In contrast, public member can be access by the code outside the class. To declare a private member, you can use private keyword followed by semi colon(:) and to declare a public member, you can use public keyword followed by semi colon(:).

Example:

class Student
{
  private:
       int stnumber;
  public:
       char *stname;
       char *stsex;
};

The protected keyword is generally used in relationship(inheritance) between classes. We don't mention it usage here. You will learn it in the Class Inheritance page where we define the relationship between classes.

Methods

A method is a member of a class that can be called to perform a specific task. Before you can call it, you must declare it between the curly brackets of the class declaration and the full method with its code outside the class.

class Student
{
  private:
     int stnumber;

     char *stname;

    char *stsex;

   public:
      void set(int stn, char *stna, char *sts);
};

void Student::set(int stn, char *stna, char *sts)
{
   stnumber = stn;

   stname=stna;

    stsex=sts;


}

Creating objects from a class


To access members of the class, you must create its object. The object can be created by writing the class name followed by the object name.

Example:

Student stu;

Class Constructors and destructors

A constructor is a special method that can be used to initialize object of the class when they are created. A destructor is a method used to clean the object up before it is destroyed. The name of constructor is the same as the name of the class. The name of destructor is the same as that of constructor except it takes ~sign.

Example:

#include<iostream.h>

class Student
{
  private:
     int stnumber;
  public:
     Student();
     ~Student();
};

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

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

A constructor can take parameters.

Example:

#include<iostream.h>

class Student
{
   private:
    int stnumber;
    char *stname;

  public:
    Student(int stn, char *stna);
    void getstudent();
   ~Student();
};



void Student::getstudent(){

  cout<<stnumber<<" "<<stname;

}

Student::Student(int stn,char *stna)
{
   stnumber=stn;
   stname =stna;
}

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


Comments

krishna negi comment

 krishna negi

dear sir i want to know about GUI with C# Pogramming so Please Help me......
if Any Information about this so please contact me mine Mail ID is krishnasinghnegi143@gmail.com,
knegi0857@gmail.com
thanking to You.......


2014-01-28



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.