Classes and Objects

C++BasicsInputArithmeticConditionsLoops → Array → Functions → Random Numbers → Fibonacci sequence → Classes and Objects

The classes and objects in C++ Construction and Examples. Classes in C++ enables us to write large programs. In classes variables are private which are accessible through public functions.

Classes and Objects: Construction

class Name //class name starts with capital letter
{
private:
variable(s) //variables are private
public:
function(s) //functions are public
}; //semi-colon in the end

Consider the following simple Circle class with only one private variable. We will access the private variable through public functions.

#include <iostream>
using namespace std;
class Circle
{
private:
int radius;
public:
void setRadius(int rad) //one parameter
{
radius = rad; //to access the variable
}
void showRadius() //to display the variable
{
cout << "Radius = " << radius << endl;
}
};
int main()
{
Circle c1; //Circle is class, c1 is object
c1.setRadius(3); //one parameter
c1.showRadius(); //display variable
return 0;
}

In the above example the function void setRadius() is used to access the private variable, and void showRadius() is used to display the variable. In the main function, c1 is object. Later we learn that we can make many objects for the same class.

In the above example, we set the radius to 3 ourselves. We can also ask user ti input the value of radius. For example, consider the following class

#include <iostream>
using namespace std;
class Circle
{
private:
int radius;
public:
void getRadius() //no parameter
{
cout << "Enter Radius: "; //to get the variable
cin >> radius;
}
void showRadius() //to display the variable
{
cout << "Radius = " << radius << endl;
}
};
int main()
{
Circle c1;
c1.getRadius();
c1.showRadius();
return 0;
}

In the next example, we are going to combine the above two classes in one class. We ourselves set the one radius, and will ask the user to input the other variable.

#include <iostream>
using namespace std;
class Circle
{
private:
int radius;
public:
void setRadius(int rad)
{
radius = rad;
}
void getRadius()
{
cout << "Enter Radius: "; cin >> radius;
}
void showRadius()
{
cout << "Radius = " << radius << endl;
}
};
int main()
{
Circle c1, c2; //two objects
c1.setRadius(5);
c2.getRadius();
c1.showRadius();
c2.showRadius();
return 0;
}

We can make any number of objects we want. For example, in the next example we are going to make three objects. In the first two objects we set the radius ourselves, and in one object we ask user to input radius. In this example the variable type is float, so now, the user can also input decimal values.

#include <iostream>
using namespace std;
class Circle
{
private:
float radius; //variable type is float
public:
void setRadius(float rad) //float parameter
{
radius = rad;
}
void getRadius()
{
cout << "Enter Radius: "; cin >> radius;
}
void showRadius()
{
cout << "Radius = " << radius << endl;
}
};
int main()
{
Circle c1, c2, c3; //three objects
c1.setRadius(3.2);
c2.setRadius(4.5);
c3.getRadius();
c1.showRadius();
c2.showRadius();
c3.showRadius();
return 0;
}

NEXT: Operator Overloading