A class is a user-defined data type in C++ that represents a blueprint for creating objects. It defines a set of attributes (data members) and methods (member functions) that operate on those attributes. Objects are instances of classes, and each object created from a class has its own set of attributes while sharing the same methods defined in the class.
Class provides a way to model real-world entities in a program.
class NameOfClass { private: // Private members (variables and functions) variable declaration; function declaration; public: // Public members (variables and functions) variable declaration; function declaration; protected: // Protected members (variables and functions) variable declaration; function declaration; };
Imagine you're working at a school and need to register a new student. Here's how you can use the Student class:
Student newStudent;
newStudent.id = studentId; // Replace with actual ID number newStudent.name = studentName; // Replace with actual name
cout << "New student information:" << endl; newStudent.display();
#include <iostream> #include <string> using namespace std; class Student { public: // Data members (properties of a student) int id; string name; // Function to display student information (member function) void display() { cout << "ID: " << id << endl; cout << "Name: " << name << endl; } }; int main() { // Create an object (instance) of the Student class Student student1; // Set the data members of the student object student1.id = 123; student1.name = "John Doe"; // Call the member function to display student information student1.display(); return 0; }
ID: 123 Name: John Doe
#include<iostream> using namespace std; class sample { public : // A variable which is declared as static known as static variable. //const // c.t.e bec ++count can't change. static int count;//=6; // only single memory will allocate for all object public : void display();// declaration of a member function }; //int sample ::count=5; // static data definition and initialization int sample ::count; // if you do not assign any value than a default value 0 will assign void sample ::display() { ++count; cout<<"The value of count : "<<count<<endl; } int main() { sample c1,c2,c3; c1.display(); c2.display(); c3.display(); }
The value of count : 1 The value of count : 2 The value of count : 3 -------------------------------- Process exited after 0.09944 seconds with return value 0 Press any key to continue . . .
#include<iostream> using namespace std; class Account { static int count;// static variable declaration public : static void display() // static member function { count++; cout<<"The value of count is :"<<count<<endl; } }; int Account ::count=56;// static data definition int main() { //Account obj1,obj2; // No need of an Objects Account::display(); Account::display(); }
The value of count is :57 The value of count is :58 -------------------------------- Process exited after 0.116 seconds with return value 0 Press any key to continue . . .
#include<iostream> using namespace std; class A { //private: int roll; // default scope is private char name[45]; public : void getValue(); // Function prototype void showValue() const; }; void A::getValue() // function defination { cout<<"Enter the roll number:"<<endl; cin>>roll; cout<<"Enter the name :"<<endl; cin>>name; } void A::showValue() const// Constant function { // roll=67; // its just read only // can't change in value cout<<"The Roll : "<<roll<<endl; cout<<"The name : "<<name; } int main() { A obj; obj. getValue();// function Calling cout<<"Output :"<<endl; obj.showValue();//function calling }
Enter the roll number: 101 Enter the name : Sachin Output : The Roll : 101 The name : Sachin -------------------------------- Process exited after 5.213 seconds with return value 0 Press any key to continue . . .