C++ Class and Object Example

 Code:

#include<iostream>

using namespace std;

class Human

public:

string name ="ABC";

int age = 20;

string Gender="Female";

int Info()

cout<<"Name is "<< name<<endl;

cout<<"Age is "<< age <<endl;

cout << "Gender is "<< Gender<<endl;


}

};

int main()

Human H ;

H.Info();

return 0;

}

Explanation

​"Is program mein humne niche diye gaye steps follow kiye hain:

  • Class Definition: Humne Human naam ki aik class banayi hai jo aik 'Blueprint' ki tarah kaam karti hai.
  • Public Access Specifier: Humne public keyword use kiya hai taake class ke bahar se data access kiya ja sake.
  • Attributes: name, age, aur Gender is class ki properties hain.
  • Method (Info): Ye function class ke andar banaya gaya hai jo attributes ki values ko print karta hai.
  • Object (H): main() function mein humne Human H; likh kar aik object banaya hai. Ye object hi class ki saari powers ko use karta hai."

Program Output

​"Jab aap is code ko run karenge, to aapko ye output milega:"

Name is ABC

Age is 20

Gender is Female


No comments