Class and Object in C++

Class and Object in C++:
Class:
  • Class is used to hold data members and member function i.e. variables and functions.
  • It can be easily accessed anywhere and used to reduce the reusability of the code.
For example,
  • Let’s take student,
  • Student properties are all called as variables and student performance or student-related calculations(i.e. student result based on total and it may be pass or fail) are called as a function.
Where,
The student is a class.
Student properties (variables) : Roll_num,name,age,ect.
Student behaviours(function)  : total,average,result,fees details ect.
Note:
  • If one or more variables declared within the class, called data members.
  • If one or more functions declared in within the class, called member function.
Object:
  • An object is used to access the members in the class.
  • It simply said as the short name of the class.
Syntax:
  1. <header files>
    class class_name //class declaration
    {
              Access specifiers:
              Variable_declaration;
              Method declaration or function_declaration()
              {
              //Statements
              }
    };
    void main()
    {
    //object declaration
    Class_name object_name;
    //accessing member function using object
    object_name .function_name();
    }
Note:
The class name and object name is a user-defined word.
Access specifiers:
  • C++ access specifiers are used for determining or setting the boundary for the availability of class members (data members and member functions) beyond that class.
Types of access specifiers:
  • Private – It is used to access the data member and member function within the classes.
  • Public – It is used to access the data member and member function out of the class. That means any class can be accessible.
  • Protected – It members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
 Note:
  • More details are given in the inheritance concept. Please click the below link to aid for more learning.
Example:
  1.  #include<iostream.h>
     #include<conio.h>
     #include<iomanip.h>//output formatting
    class std
    {
    public:
    int sno,m1,m2,m3,m4,m5,tot;
    float avg;
    char name[30];
    void get()//function for value get from user
    {
    cout<<"\n Enter student number : ";
    cin>>sno;
    cout<<"\n Enter student name : ";
    cin>>name;
    cout<<"\n Enter Five subject mark : ";
    cin>>m1>>m2>>m3>>m4>>m5;
    }
    void result()//fuction for calculate total and average
    {
    tot = m1+m2+m3+m4+m5;
    avg =tot/5;
    }
    void display()//function for displaying full details
    {
    cout<<"\n Student details............";
    cout<<"\n Student id is : "<<sno;
    cout<<"\n Student name is : "<<name;
    cout<<"\n Five subject marks : "<<m1<<setw(5)<<m2<<setw(5)<<m3<<setw(5)<<m4<<setw(5)<<m5;
    cout<<"\n Total mark for five subject :"<<tot;
    cout<<"\n Average mark for five subject :"<<avg;
    }
    };
    void main()
    {
    clrscr();
    std s;//object declaration
    //accessing member function using object
    s.get();
    s.result();
    s.display();
    getch();
    }
Output:
Note:
If you need details about iomanip.h header file and setw() function, click the below link to learn.

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me