OOP

Class

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).

1. Class Declaration:

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).

2. Member Variables (Data):

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 ...
};

3. Member Functions (Behavior):

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;
  }
};

4. Practice:

#include<iostream>
using namespace std;