class in c++ - InTerestinG321

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 above program “int roll.no, char name[20], int age” are private.
To make parts of a class as public, you can define that 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 instance of a class

When you declare an object of a class, you can create an instance of a class, In this case mystack is a 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 no\n”;
Cin>>roll.no
Cout<<”enter student name\n”;
Cin>>name;
Cout<<”enter age of the student\n”
Cin>>age;
}
 Void student::display();
{
Cout<<”student roll no is\n”<<roll.no;
Cout<<”name of the student is\n”<<name;
Cout<<”age of the student is\n”<<age;
}
Int main()
{
Student ob;
Ob.read();
Ob.display();
}

Function overloading
General syntax
Friend function

1 comment: