Class and Object in C++
C++ Programming
Class and Object in C++
The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects
First we create a class which store the name string data type, marks integer data type, result as string and grade as string.
class student { public: string sname;// name of the student int marks; // marks of students string result; // students result };
In the above class student we declare three variables which store the information of about the student.
Now we declare the object of that class.
Student info; // Declare a object variable info of student class // (where student is class / user define data type)
Now we write a complete program:
#include using namespace std; class student { public: string sname; // name of the student int marks; // marks of students string result; // students result }; int main() { student info;// object of class student // get the information cout<<"Enter the Student name"< >info.marks; cout< 40) info.result="Pass"; else info.result="Fail"; // dipslay the information on screen cout << "Your Information : " <