What is class in cpp programming


Definition : Class is a template or blue print from which an object is created.

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.



Syntex of class in cpp

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



Class Structure Explaination:

  • class NameOfClass: This line begins the declaration of a class named NameOfClass. A class in C++ is a blueprint for creating objects.
  • private:, public:, protected: These are access specifiers that determine the visibility of the class members.
    • private: members are accessible only within the class.
    • public: members are accessible from outside the class.
    • protected: members are accessible within the class and by derived classes.
  • variable declaration: This line represents the declaration of a class member variable. Variables declared within each access specifier block belong to that access level.
  • function declaration: This line represents the declaration of a class member function. Functions declared within each access specifier block belong to that access level.


Imagine you're working at a school and need to register a new student. Here's how you can use the Student class:

  1. Gather student information: Ask the student for their ID number and name.
  2. Create a new Student object:
    Student newStudent;
    
  3. Set the data members:
    newStudent.id = studentId; // Replace with actual ID number
    newStudent.name = studentName; // Replace with actual name
    
  4. Display student information:
    cout << "New student information:" << endl;
    newStudent.display();
    


Here we are creating a program using above scenario for students.

#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;
}
Output:
ID: 123
Name: John Doe


Above explanation for class and object program:

  • #include <iostream> and <string>: These lines include necessary libraries for input/output (iostream) and string manipulation (string).
  • class Student { ... }: This defines the Student class.
  • public: This section defines the public members of the class, accessible from outside the class.
  • Data members: id (integer) and name (string) store information about the student.
  • Member function: display() is a function defined within the class to display student information (ID and name) using cout.
  • int main() { ... }: This is the main function where the program execution begins.
  • Student student1;: This creates an object (instance) of the Student class named student1.
  • student1.id = 123; student1.name = "John Doe";: These lines set the values of the data members for the student1 object.
  • student1.display();: This calls the display() function on the student1 object, which prints the student information.

Data Hiding


Data hiding is a restriction on the data to access outside the class.
We can hide the data using private and protected access specifier.
Example 10

Static Data members


  1. Static data member is used to provide the common property for all the object
  2. Static data member can be public or private.
  3. The static member should be created and initialized before the main control block begins.

Properties of static data members


1. whenever a static data member is declared, only one copy of that member is created for the entire class and is shared by all the objects of the class.
2. The static data member should be created and initialized before the main function.
3. By default, static data member is initialize to zero when the first object of its class is created.
4. The access rule of static data member is same as the other data members of the class.
For Example: when a static data member is declared private, the function which is not the member of the class can not access this Data member.

#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();
}
                  
Output
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 . . .
Example 11

Static member function

It also used to provide common behavior for all the objects.
1. A static function can access and manipulate only on static data member of the class.
2. A static member function is independent of any object. Therefore a static function can be called using the class name instead of its objects as follow :
class_name :: static_function_name;

#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();
}
Output
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 . . .
Example 12

Constant Member Function

A function where we can't change any value of variables. Just we can show only.

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

Output
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 . . .