C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Classes

C++ Classes

shape Introduction

The main feature of C++ is Object Oriented Programming. It is also called as OOPS concept. The basic building blocks of object oriented programming are "Classes and Objects".

Class

shape Description

CPP Classes are similar to structures in C-language. A single C++ program can contain any number of classes. They are the user-defined types that consists of objects and is the combination of data and functions. These two are called as members of the class. Class is the blue-print of an object. It does not define any data directly but the class represents two important functions of an object such as:

shape Syntax

A class is represented with a keyword class The identifier used specifies the class name. Class body is given in {}. The class definition ends with a semicolon ;.
class class_name { data statements; function statements; };

Member Functions of class

shape Description

The function with its own definition in a class can be called as a Member Function. It is possible to access any of the object and object members present in CPP classes in which the member function is defined. A member function can be declared and used in two ways.

Inside class member functions

If the member function is defined inside the CPP classes then it is called as "Inline Functions". Even if the inline specifier is not specified, it is considered as inside class function. [c] class John_marks { public: int maths_marks; int science_marks; int social_marks; int getAverage(void); // inside class member function { return maths_marks + science_marks + social_marks; } };[/c]

Outside class member functions

These are used outside of the class with the help of scope resolution operator::. The operator is placed after the name of class.
int John_marks::getAverage(void) { return (maths_marks + science_marks + social_marks)/3; }

shape Example

Below is an example for inside and outside CPP classes member functions. [c] #include <iostream> using namespace std; class Student_marks { public: int Maths; int Science; int Social; // Member functions declaration int getAverage(void); void setMaths( int Mat ); void setScience( int Sci ); void setSocial( int Soc ); }; // Member functions definitions int Student_marks::getAverage(void) { return (Maths + Science + Social)/3; } void Student_marks::setMaths( int Mat ) { Maths = Mat; } void Student_marks::setScience( int Sci ) { Science = Sci; } void Student_marks::setSocial( int Soc ) { Social = Soc; } // Main function int main( ) { Student_marks John; Student_marks Mohan; int Average = 0.0; // John marks John.setMaths(75.0); John.setScience(85.0); John.setSocial(95.0); // Mohan marks Mohan.setMaths(76.0); Mohan.setScience(69.0); Mohan.setSocial(83.0); // Average of john marks Average = John.getAverage(); cout << "Average of john marks is : " << Average <<endl; // Average of mohan marks Average = Mohan.getAverage(); cout << "Average of mohan marks is : " << Average <<endl; return 0; }[/c] Output [c] Average of john marks is : 85 Average of mohan marks is : 76[/c]

shape More Info

Click on below tags to know more about below mentioned topics of C++. Constructors in C++ : A constructor is a kind of member function that initializes an instance of its class. Copy constructors : A copy constructor is a special constructor for creating a new object as a copy of an existing object. friend functions,this pointer,static functions : A friend function is defined outside that class scope but has the right to access all private and protected members of the class. The member functions of C++ classes will have the this pointer to point to the address of class objects. Static member functions are independent of objects of the class and can be called without objects also

Summary

shape Key Points

  • CPP Classes are combination of data and functions
  • Class is blue-print of an object having all its variables and methods
  • Function with own definition in class is called as Member Function
  • Constructor does not have any return-type. Default and parameterized are types of constructors
  • This pointer points to the address of class objects

shape Programming Tips

When a destructor is defined for a class, it destroys all of the space associated with the class.