Check out example codes for "class in c++". It will help you in understanding the concepts better.

Code Example 1

// C++ program to demonstrate function  
// declaration outside class 
  
#include <bits/stdc++.h> 
using namespace std; 
class Geeks 
{ 
    public: 
    string geekname; 
    int id; 
      
    // printname is not defined inside class definition 
    void printname(); 
      
    // printid is defined inside class definition 
    void printid() 
    { 
        cout << "Geek id is: " << id; 
    } 
}; 
  
// Definition of printname using scope resolution operator :: 
void Geeks::printname() 
{ 
    cout << "Geekname is: " << geekname;  
} 
int main() { 
      
    Geeks obj1; 
    obj1.geekname = "xyz"; 
    obj1.id=15; 
      
    // call printname() 
    obj1.printname(); 
    cout << endl; 
      
    // call printid() 
    obj1.printid(); 
    return 0; 
}

Code Example 2

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

Code Example 3

// C++ program to demonstrate  
// accessing of data members  
  
#include <bits/stdc++.h> 
using namespace std; 
class Geeks 
{ 
    // Access specifier 
    public: 
  
    // Data Members 
    string geekname; 
  
    // Member Functions() 
    void printname() 
    { 
       cout << "Geekname is: " << geekname; 
    } 
}; 
  
int main() { 
  
    // Declare an object of class geeks 
    Geeks obj1; 
  
    // accessing data member 
    obj1.geekname = "Abhi"; 
  
    // accessing member function 
    obj1.printname(); 
    return 0; 
}

Code Example 4

class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area (void);
} rect;

Code Example 5

/*
 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.
   }
   ......
}

Code Example 6

class Name {
  private:
  	int data;
  
  public:
  
    // Constructor
  	Name() {
		// Code      
    }
  	int id;
  	void fun1(int a) {
        // Some instructions here 
    }
 	 
}

Learn ReactJs, React Native from akashmittal.com