Howdy Doe
0
Q:

c++ public class declaration


  class MyClass {       // The class
  public:             
  // Access specifier
    int myNum;        // 
  Attribute (int variable)
    string myString;  // 
  Attribute (string variable)
};

int main() {
  MyClass myObj;  
  // Create an object of MyClass

  // Access attributes and set values
  myObj.myNum 
  = 15; 
  
  myObj.myString = "Some text";

  // Print attribute values
  
  cout << myObj.myNum << "\n"; 
  cout << myObj.myString; 
  return 0;
}

  
1
class class_name {
  access_specifier_1:
    member1;
  access_specifier_2:
    member2;
  ...
} object_names;
0
/*
 Keyword "this"
You can use keyword "this" to refer to this instance inside a class definition.

One of the main usage of keyword this is to resolve ambiguity between the names of 
data member and function parameter. For example:
*/
class Circle {
private:
   double radius;                 // Member variable called "radius"
   ......
public:
   void setRadius(double radius) { // Function's argument also called "radius"
      this->radius = radius;
         // "this.radius" refers to this instance's member variable
         // "radius" resolved to the function's argument.
   }
   ......
}
0

New to Communities?

Join the community