Profile

Join date: May 10, 2022

About

С++ Training: Class

Class is a user defined data type. The variables that are declared inside a class are called data members and functions are called member functions. Data members and member functions are collectively called class members. Data Encapsulation means to wrap the data and method into a single unit. It provides a template to objects. According to homework helper any number of objects can be produced from a class.

• Declaration of Class

class ClassName{

private:

Variable declaration;

Function declaration;

public:

Variable declaration;

Function declaration;

};

According to free homework answers service here Class_Name is the name of the class, ‘private’ variables and functions can be accessed only within the class, ‘public’ variables and functions can be accessed outside the class.

The Variables are ‘private’ by default.

For example -

class Rectangle{

private: float length; /* variable declaration */ float breadth; /* variable declaration */ float area; /* variable declaration */

public: int area(float l, float b); /* Function declaration */

}; /* class ends with a semicolon */

• Access Modifiers:

Keywords -

i. public – accessible to everybody. ii. protected - accessible to derived classes. iii. private - accessible to same class only.

• Object :

According to chemistry homework help service object is an instance of a class. It is used to access the members of a class. Without an object class is not accessible. The memory space is allocated separately to each object for their data members.

For example -

Rectangle r1, r2; /* object declaration */ This will create two objects of class Rectangle r1 and r2. Memmory allocated to a object = sumof size of all data members

Accessing Class members

The class members can be accessed using objects. It can be accessed by object_name.class_member_name; Here object_name is the name of the object followed by ‘. (dot)’ and class_member_name which is the member to be accessed. For example - Consider above class example Rectangle r1, r2; /* object declaration */

r1. area( l, b);

• Defining a function : A member function can be defined in two ways – - Inside the class definition - Outside the class definition Inside the class definition Function can be defined inside a class at the time of declaration. For example - class Rectangle{

float length; float breadth;

public: void display() { /* Function definition inside a class */

cout<< “Length - ” <<length; cout<< “Breadth - ” <<breadth; } }; Outside the class definition The Scope resolution operator ‘::’ is used to define function outside a class. Declaration of function outside the class - return_type Name_of_the_Class :: Function_name (argument list){

body of function

} For example - void Rectangle :: display() { /* Function definition outside the class*/

Function Body

More resources:

Data types in C++

Tokens in C++

С++ Training

С Training: Operators in C

С Training: Variables in C


Willie Miller

More actions