class in c++

Class: Class is a collection of object, Class is called using the keyword class.

General form:Class class-name
{
Private data and functions
Public:
Public data and functions
}object name list;

EX:
#include<iostream>
Using namespace std;
Class student
{
Int roll.No;
Char name[20];
Int age;
Public:
Void read();
Void calc();
Void display(int i);
};
Note: a class contain private as well as public parts. Default defined parts in a class are private, private parts are cannot access by any function because that is not a member of a class. In the above program “int roll.no, char name[20], int age” are private.
To make parts of a class as public, you can define parts, after the public keyword, all variable and functions accessed the parts which are defined after the public keyword. In above program “void read(), void calc(), void display(int i)” are public.
Once you defined a class you can create an object of that class by using the class name.

EX:  stack mystack

Object: Object is an instance of a class

When you declare an object of a class, you can create an instance of a class, In this case, my stack is an instance of a stack.
The:: is a scope resolution operator. that says the compiler read() belongs to the student class.
Declaration.

“ Void student::read() ”

Ex:

#include<iostream>
Using namespace std;
Class student
{
Int roll.no;
Char name[20];
Int age;
Public:
Void read();
Void display();
};
Void student :: read()
{
Cout<<”enter reg non”;
Cin>>roll.no
Cout<<”enter student namen”;
Cin>>name;
Cout<<”enter age of the studentn”
Cin>>age;
}
 Void student::display();
{
Cout<<”student roll no isn”<<roll.no;
Cout<<”name of the student isn”<<name;
Cout<<”age of the student isn”<<age;
}
Int main()
{
Student ob;
Ob.read();
Ob.display();
}

Recommended Posts:

0 thoughts on “class in c++”

Leave a Comment