Classes in C++ Programming



Introduction to Classes in C++

A class is a programmer-defined data type that describes what an object of the class will look like when it is created.

It consists of a set of variables and a set of functions. Here is the general format of a class declaration.

Syntax:


    // Class declaration begins with,  the key word class and a name.
    class ClassName 
    { 
    Declarations for class member variables
    and member functions go here.
    }; // Notice the required semicolon.

Once the class has been designed, the next step is to write the class declaration. This tells the compiler what the class includes.

Notice that the class name begins with a capital letter. Although this is not strictly required, it is conventional to always begin class names with an uppercase letter.

Placement of private and public members

It does not matter whether we list the private or public members first. In fact, it is not even required that all members of the same access specification be declared together.

Both examples below are legal declarations of the Circle class.


  class Circle
   {
    public:
    void setRadius(double r)
    void setRadius(double r)
    double getArea()  
       { 
  return 3.14 * pow(radius, 2); 
    }
    private:
    double radius;
    };

  class Circle
  { 
  public:
  void setRadius(double r) { 
  radius = r; 
    }
    double radius;
    private:
    public:
  double getArea() { 
    return 3.14 * pow
    (radius, 2); }
    };

Defining and testing Class

Our example redefines class GradeBook with a display- Message member function that displays the course name as part of the welcome message.

The new version of display Message requires a parameter that represents the course name to output.


    #include <iostream>
    #include <string> // program uses C++ standard string class
    using namespace std;
    // GradeBook class definition
    class GradeBook
    {
    public:
    
    // function that displays a welcome message to the GradeBook user
    void displayMessage(string courseName )
    {
    cout << "Welcome to the grade book for\n" << courseName<<"!"<< endl;
    } // end function displayMessage
    }; // end class GradeBook 
    
    // function main begins program execution
    int main()
    {
    string nameOfCourse;
    GradeBook myGradeBook; //create a GradeBook object named myGradeBook
    
    // prompt for and input course name
    cout << "Please enter the course name:" << endl;
    getline( cin, nameOfCourse ); // read a course name with blanks
    cout << endl; // output a blank line
    
    // call myGradeBook's displayMessage function
    // and pass nameOfCourse as an argument
    myGradeBook.displayMessage( nameOfCourse );
    } // end main

    Output:
    Please enter the course name:
    CS10 Introduction to C++ Programming
    Welcome to the grade book for
    CS10 Introduction to C++ Programming!

The #include <string> header must be included in the program to use function getline, which belongs to namespace std.


Ads Right