MOCKSTACKS
EN
Questions And Answers

More Tutorials








C++ Class and Object

Class is In object-oriented programming, it is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

C++ Class Syntax

class class_name {       // The class
 public:             // Access specifier
    attribute1_type attribute1_name;        // Attribute
    attribute2_type  attribute2_name;  // Attribute 
    attribute3_type  attribute3_name;  // Attribute 
    attribute4_type  attribute4_name;  // Attribute 

 public:             
    void method1() {  // Method/function defined inside the class
      //code to be executed
    }

    return_type method2() {  // Method/function defined inside the class
      //code to be executed
    }
};


For example, a class could be a car, which could have a color field, make field and model field. Another related class could be a truck, which would have similar fields, but not be exactly the same as a car. =

C++ Example of creating a class

class Car {        
 public:              
    string color;        
    string make;         
    string model;
 public:             
    string getColorAsMessage() { 
      return "My color is: " + color;
    }
};

Another C++ Example of creating a class

class Truck {        
  public:              
    string color;        
    string make;         
    string model;
};

C++ Create an object from class

To create an object we need to specify the type as Class name and define the name of the object. All public attributes and functions can be accessed than by using dot (.).

See below example for creating an object from class in C++:

int main() {
  Car newCar;  

  newCar.color = "Red";
  newCar.make = "BMW";
  newCar.model = "X5";

  cout << newCar.getColorAsMessage() << "\n"; 
  cout << newCar.make; 
  return 0;
}



After learning above concepts it's now time to put all codes together and create our output:

#include <iostream>
#include <string>
using namespace std;

class Car {        
 public:              
    string color;        
    string make;         
    string model;
 public:             
    string getColorAsMessage() { 
      return "My color is: " + color;
    }
};

class Truck {        
 public:              
    string color;        
    string make;         
    string model;
};


int main() {
  Car newCar;  

  newCar.color = "Red";
  newCar.make = "BMW";
  newCar.model = "X5";

  cout << newCar.getColorAsMessage() << "\n"; 
  cout << newCar.make << "\n"; 
  
  
  Truck newTruck;  

  newTruck.color = "Red";
  newTruck.make = "BMW";
  newTruck.model = "X5";

  cout << newCar.color << "\n"; 
  cout << newCar.make << "\n"; 
  return 0;
}

Output

My color is: Red
BMW
Red
BMW

Conclusion

In this page (written and validated by ) you learned about C++ Class and Object . What's Next? If you are interested in completing Cpp tutorial, your next topic will be learning about: Cpp Encapsulation.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.