C++ Classes

A class serves as an object function Object() or "blueprint" for making objects and is a user-defined data type that we may utilise in our application.

In essence, attributes and methods are class-specific variables and functions. These are sometimes called "class members."

To create a class, use the class keyword:


  class Car {

      public:

      string brand;
      string model;
      int year;

      private:
      double speed;
      double speedFactor;

      double calculateSpeed(){
          return speed * speedFactor;
      }
      };

What's a class and how can we build it in C++


  • You can build a class called Car by using the class keyword.

  • A class can hold variables, we call them attributes, and Functions, we call them methods. We call them both class members

  • The public keyword signifies that a class's members (attributes and methods) are accessible to users outside of the class. Meaning they are accessible and visible to objects as well .

  • private section in a class, indicates that this method, or attribute inside it, can only be accessed by class members. So if we create an Objects and we try to directly call those members, it won't work. Meaning they will be hidden. Unlike when we have Public Members
  • Attributes are used to describe variables that are defined inside of a class.

  • Finally, add a semicolon (;) at the end of the class definition.





© Copyright 2022 Advanced PLC. All rights reserved.