Following section provide a step by step break down of the syntax of a class with the help of an example. In this example, Distance has two private member variables: feet (an integer) and inches (a float). It also has two public member functions: setFeet (to set the feet) and getFeet (to retrieve the Feet).
class Distance {
// ... class definition goes here ...
};
This line declares a class named Distance. The curly braces {} mark the beginning and end of the class definition, where you’ll specify the class members (data and functions).
class Distance {
private:
int feet; // Member variable to store an integer
float inches; // Member variable to store a floating value
public:
// ... member functions go here ...
};
private: This keyword defines member variables that are only accessible within the class itself. These variables encapsulate the data and protect it from being modified directly from outside the class.public: This keyword defines member variables that can be accessed from outside the class using the object’s name and the dot operator (.).class Distance {
private:
int feet;
float inches;
public:
void setFeet(int newValue) { // Function to set the value
feet = newValue;
}
int getFeet(){ // Function to get the feet
return feet;
}
};
private (accessible only within the class) or public (accessible from outside the class).setInches() to set the value of inchesgetInches() to get the value of inches#include<iostream>
using namespace std;
Distance.cpp