C++ Classes

Attention Mobile Users ! This page works best in the desktop mode . Click Here : http://hscboards.wikia.com/C%2B%2B\_Classes?useskin=oasis

A Class in C++ is a collection of functions and properties . Using classes , we can assign more than one value or property to a single variable . Such a variable is called as an Object of the Class .

## Specifying a Class

**Class Declaration :** << class Name >> e.g. class Ratio

class Ratio

{

Class Working

} **;** // semicolon is needed at the end of Class Working

**Class Function Declaration :** << return type Name (Arguments) >> e.g. void Read (int num , int den)

**Private & public :** A class may have a private and a public section . The variables & function in private section can be accessed by variables of that class Only and not by main() function . The public section can be accessed by all variables in the main() .

**Creating Objects :** << Class\_Name Object\_Name >> e.g. A.Ratio is an object of class Ratio . All the variables and functions in class Ratio can be used for Object A .

**Using Class Functions :** << Object\_Name**.**Function() >> e.g. **A.Read()** will run the "read" function for object A .

## Accessing Members of a Class

Here's how to link the class with main() function by accessing members of a class .

**Calling a member Function :** << Object\_Name**.**Function() >> e.g. **A.Read()** will run the "read" function for object A .

## Defining Member Functions

Member functions in the public section can be directly used in the main() section .These functions can either be declared **inside** the class or **outside** the class .

**Inside the Class :** (Treated as an inline function )

return\_type Function\_Name (Arguments)

{

Function Working

}

**Outside the Class :** (To make it inline function , add "inline" before return\_type)

return\_type class\_name **::** function\_name (Arguments)

{

Function Working

}

**Properties of Member Functions :**

1. Several different classes can use the same function name .
2. Member Functions can access private section of class .
3. A member function can call another member function directly , without using the dot operator

## How to construct a C++ Program with Classes

It's quite simple . Up to 11th std. , we used to write our entire program under the main () section . Now we shift all our variables and functions to classes and create objects in the main () function . These objects act as a link between our class and main() function .

**Example of a C++ Class Program** (This is a crucial part in being able to write C++ programs with Classes e A program that accepts values of numerator and denominator and displays it as a ratio .

class Ratio

{ int num ; int den ;

public :

void read (int a , int b )

{ num = a ; den = b ; }

};

void Ratio :: display (void)

{ cout << num << " / " << den << endl ; }

int main ()

{ Ratio A ;

A.read (22,7) ;

A.display() ;

return 0;}

class Ratio declared

Variables num and den can only be used by Members of class Ratio

Function Read Declararation and working

This function accepts the value entered through main() function

Outside the class declaration of Function print .

This function prints the Ratio

main() Function declared

Object A of class Ratio created

read function is performed for object A . Thus , object A now has values 22 & 7 .

display function displays th values as a ratio e.g. 22/7

Nesting of Member Functions

Arrays within a Class

## Static Data Members

A static data member is a variable of a class which is qualified as static . It's value remains the same throughout the program .

Properties :

1. It is initialized to zero when the first object of its class is created . No other initialization is permitted .
2. Only one copy of that member is created for the entire class and is shared by all the objects of that class .
3. It is visible only within the class , but its lifetime is entire program .

## Friend Functions

The private section of a class can be accessed only by member functions of the class . But , outside functions can also access private section of a class by using Friend Functions .

Syntax :

class Ratio

{ int num ; int den ;

public :

friend void xyz(void);

};

In this program , **xyz** is a function defined outside the body of the class which does not belong to the class . Thus , we say the function xyz is friendly to the class Ratio .

Properties :

1. It does not belong to the class , in which it is declared .
2. It cannot be called using objects .
3. It can be called in the main function , without using objects like a normal function .
4. It cannot access member names directly . It has to use an object to dos o . e.g. A**.**x
5. It can be declared in private or public part without changing its name .
6. Usually , it has objects as arguments .

## **Constructors**

Constructors are the functions of a class , which : 1) have no return type 2)have same name as the Class Name .

Constructors cannot return a value , thus they are only used for accepting values from the main() function like A**.**read() in the above example .

**with parameters**

A constructor can have parameters so that it can accept values , similar to the read function in the above example .

**Multiple constructors**

A class can have multiple constructors by having different number of parameters or arguments or different data types of parameters (Just like function overloading) .

**overloaded constructors**

Similar to Function Overloading

**default arguments**

Arguments can be given default values while declaring them with constructor .

**dynamic initialization**

Argument values can be provided during run time .

## Destructors

Syntax : ~class\_name () { }

The detructor is used to delete the objects that are created by constructor . Using a destructor is a good practice since it releases memory space for furthur use .

## **Operator Overloading**

Syntax : return\_type class\_name  :: operator op(Argument list) { }

e.g. int Ratio :: operator + (Ratio A) {}

; where Ratio is a class ; Ratio A is constructor for object A .

Rules for overloading operators :

1. Only existing operators may be overloaded .
2. Using operator pverloading , you cannot overload operators and change their meaning . e.g. plus sign cannot be used to subtract 2 numbers .
3. The overloaded operator have atleast one operand of user defined data type .
4. Overloaded operators follow syntax of original operators
5. The following operators cannot be overloaded :
	1. **. - Membership operator**
	2. .\* - Pointer Member operator
	3. :: - Scope Resolution operator
	4. ?: - Conditional operator
6. Following operators cannot be overloaded by using friend functions but using member functions only
	1. = Assignment operator
	2. () Function Call Operator
	3. [] Subscripting Operator
	4. -> Class Member Access Operator
7. Unary operators overloaded by means of membr function take no explicit arguments and return no explicit values

## **Inheritance**

Inheritance is the property of classes to inherit properties from each other .

**Base Class :** The Base class shares its properties with other classes .

**Derived Class :** The Derived Class inherits properties from Base class .

**Syntax :** class derived\_class\_name  : visibility\_mode base\_class\_name

**e.g.** class Fraction : private Ratio // class Fraction can inherit properties of class Ratio private section

class Fraction : public Ratio // class Fraction can inherit properties of class Ratio public section

class Fraction : Ratio // private by default

**Single Inheritance**

A single class inherits properties from a single base class . e.g. B inherits properties from A . A ---> B

**Multilevel Inheritance**

A single class B inherits properties from a single base class A . Another class C inherits properties from Derived Class B . e.g. A--->B--->C

**Multiple Inheritance**

A single class derives properties from two or more different classes . e.g. class D derives properties from classes A , B and C .

**Hierarchical Inheritance**

Two or more classes derive properties from a single class . All properties of base class are inherited to sub-classes .

e.g. class B , C , D inherit properties from class A . class E , F , G inherit properties from class C . Thus , classes B ,C ,D , E ,F ,G will have properties of class A .

**Hybrid Inheritance**

A mixture of two or more types of inheritance is called as hybrid inheritance .

## Virtual Base Classes

Som times , a class inherits properties from a single class from two different paths . for e.g. class D inherits properties from class B and class C . Now , class B and C inherit properties from class A . THus , class C inherits propertis from class A twice , through class B and C .

THis problem can be solved by making a base class **virtual** .