C/C++

C++ class with programming examples

C++ Class:

C++ Class- The most important feature of the C++ programming language is that it supports object-oriented programming (OOP). In OOP, the computer program is divided into objects. OOP language is an easy and flexible approach for designing and organizing the program. The program is designed by using classes. In this article, the concept of objects and classes has been discussed.

A class is a collection of data functions. The data items and functions are defined within the class. The functions are written to work upon the data items and each function has a unique relationship with the data items of the class.

Classes are defined to create user-defined data types. These are similar to built-in data types available in all programming languages.

The definition of a data type does not create any space in computer memory. When a variable of that data type is declared, memory space is reserved for that variable. Similarly, when a class is defined, it does not occupy any space in the computer memory. It only defines the data items and the member function that can be used to work upon its data items. Thus defining a class only specifies its data members and the relationship between the data items through its functions.

Classes and structures are similar. The syntax of a structure and a class is also similar. But generally, structures are exclusively used to hold data, and classes are used to hold both data and functions.



Defining a C++ Class:

A class is defined in a similar way as a structure is defined. The keyword “class” is use to define the class. The general syntax to define a class is

class class_name

{

Body of the class

};

Where:

Class

          It is the keyword that is used to define a class.

Class_name

          It represents the name of class. The objects are created by this name. the rules given the name of a class and a variable are same. Any word that can be a variable name in a C++ program can be a class name.

Body of class

          The body of a class consists of the data items and the functions. These are called members of the class these are written between braces.

Semicolon (;)

          The body of a class ends with a semicolon.

Members of a C++ class:

A class contains data items and functions. These are called members of the class. The data items are called data members and the functions are called member functions.


Data member of a C++ class:

The data items of a class are called data members of the class. For example, a class that has four integer type and two float type data items is declared as:

class abc

{

int w,  x,  y, z;

float a, b;

} ;

In the above c++ class a, b, w, x, y and z are data members of the class “abc”.

Member Functions of the C++ class:

The functions of a class that are defined to work on its data members are called member functions of the class. The member functions may be defined within the class or outside it.

For example:

Class xyz

{

Private:

          Int a, b, c;

Public:

          Void get(void)

{

Cout<<”enter value of a , b and c”;

Cin>>a>>b>>c;

}

Void pout(void)

{

Cout<<”a=”<<a<<endl;

Cout<<”b=”<<b<<endl;

 

Cout<<”c=”<<c<<endl;

 

}

};

In the above c++ class, there are three data member and two member functions. The member functions are “get” and “pout”. The “get” function is used t input values into data members a, b, and c. the “pout” function is used to print values of the data members on the computer screen.


Member Access Specifiers in class:

The commands that determine whether a member of a c++ class can be accessed from outside the class or not are called member access specifiers.

Normally two types of member access specifiers are used. These are:

  • Private:
  • Public:

Private Specifier:

The members of a class that can be accessed only from within the class are called private members. They cannot be accessed from outside that c++ class.

Normally all data members are declared as private. The member functions can also be declared as private. The members that are made private can only be accessed from within the c++ class.

All class members that come after the specifier private: and upto the  next member access specifier are declared as private and are accessible only from inside the c++ class. The default access mode is private. Ths if no access specifier is given, the members are treated as private.

Making a data to be accessed from within the class is called data hiding, i.e. the data declared as private is hidden from outside the c++ class.

Public Specifier:

Public members of a c++ class can be accessed both from inside and from outside the class. Normally, member functions are declared as public. The data members can also be declared as public.



Example how to use private and public access specifier in c++:

output:

In the above example, the c++ class has been defined its name is “cdate”. It has three data members y, d, and m of int type and has been declared as private. These data members can only be accessed from inside this class.

The class also has two members function “gdate” and “pdate”. The “gdate” function gets the date and “pdate” function prints the date in date format (dd/mm/yyyy) on the computer screen. Both the function members are declared as public.


Example how to make feet to inches converter using c++ class :

Output:

In the above example, the class has been defined its name is “converter”. It has two data members feet and inches of int type and has been declared as private. These data members can only be accessed from inside this class.

The class also has two member functions “get_data” and “show_data”. The “get_data” function gets the data from the user and “show_data” function prints the distance in inches on the computer screen. Both the function members are declared as public.


Example how to use more than two objects to access the data item of the class:

Output:

Differences between a structure and a C++ class:

In C++ The keyword “class” is use to define the class. The class has private members and base classes by default. In C++ The keyword “struct ” is use to define the Structure. Its members and base classes are public by default. In practice, structs are typically reserved for data without functions.



Class Example:

Output:

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button