C++ Programming

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.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

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++:

#include <iostream>
using namespace std;
class cdate
{
    private:
    int y, d, m;
    public:
    void gdate(void)
    {
        cout<<"Year :"; 
        cin>>y;
        cout<<"Month :"; 
        cin>>m;
        cout<<"Day :"; 
        cin>>d;
    }
    void pdate(void)
    {
        cout<<"Date is : ";
        cout<<d<<"/"<<m<<"/"<<y;
    }
};

int main()
{
    cdate date;
    date.gdate();
    date.pdate();
    

    return 0;
}

output:

Year :2020
Month :12
Day :3
Date is: 3/12/2020

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 :

#include <iostream>

using namespace std;
class converter
{
    private:
    float feet,inches;
    public:
    void get_data(void)
    {
        cout<<"Enter in feet :"; 
        cin>>feet;
        
    }
    void show_data(void)
    {
        inches = feet*12.0;
        cout<<"Distance in inches: "<<inches;
    }
};

int main()
{
    converter data;
    data.get_data();
    data.show_data();
    

    return 0;
}

Output:

Enter in feet :3
Distance in inches: 36

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:

#include <iostream>

using namespace std;

class MyCar {
  public:
    string car_brand;   
    string car_model;
    int car_year;
};

int main()
{
     MyCar Obj1;
  Obj1.car_brand = "Toyota";
  Obj1.car_model = "Gli";
  Obj1.car_year = 2018;

  // Create another object of Car
  MyCar Obj2;
  Obj2.car_brand = "Honda";
  Obj2.car_model = "reborn";
  Obj2.car_year = 2015;

  // Print attribute values
  cout << Obj1.car_brand << " " << Obj1.car_model << " " << Obj1.car_year << "\n";
  cout << Obj2.car_brand << " " << Obj2.car_model << " " << Obj2.car_year << "\n";

    return 0;
}

Output:

Toyota Gli 2018
Honda reborn 2015

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:

#include <iostream>

// create a new class called Person
class Person
{
// public variables and functions
public:
    // class constructor
    Person();

    std::string name;
    int age;

    void setName(std::string);
    void setAge(int);
};

// define the class constructor
Person::Person()
{
    // initialize some variables...
}

// define the setName() function in the Person class
void Person::setName(std::string userName)
{
    // set the name variable in the Person class to the given argument userName
    name = userName;
}

// define the setAge() function in the Person class
void Person::setAge(int userAge)
{
    // set the age variable in the Person class to the given argument userAge
    age = userAge;
}

int main(void)
{
    // create a new person named Khan
    Person khan;

    // define khan
    khan.setName("Khan");
    khan.setAge(27);

    // display khan's age
    std::cout << khan.name << " is " << khan.age << " years old." << std::endl;
}

Output:

Khan is 27 years old.

 

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button