Introduction to classes
Importantfeature of C++
Bjarne Stroustrup named ‘C with classes’
Features provided
Abstraction
Encapsulation
Inheritance
Polymorphism
3.
Need for Class
Wayof binding the data and its associated
functions together
Used to create user defined datatype
General Form
class classname
{
private:
variabledeclaration;
function declaration;
protected:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
Definition of classmembers
Example
class result
{
private:
char name[10];
int rollno,mark1,mark2,mark3,total;
public:
void accept();
void display();
};
Data
members
Member
functions
8.
Defining methods ofa class
Inside the class definition(Inline member
function)
Outside the class definition(outline/non-inline
member function)
Without defining function, class definition
will become incomplete
Defining outside the class uses Scope
Resolution Operator(::)
9.
Syntax for definingoutside the
class
returntype classname::functionname(list)
{
function definition
}
Example:
void add::display()
{
function definition
}
10.
Creating objects
To makeuse of a class, the variables of that
class type have to be declared
The class variables are called as objects(instance
of class)
Example:
student s;
instance of class student
11.
Methods for creatingobjects
Global object
Declared outside all the functions or placed
immediately after the closing brace
Local object
Declared within a function and it cannot be
accessed from outside the function
12.
Methods for creatingobjects -
Example
class add
{
------
------
}a1; //global object
add a2; //global object
int main()
{
add a3; //local object
--------
}
13.
Memory allocation ofobjects
Memory space is allocated for data members when the
objects are created
No memory is allocated for member functions since all
the objects use the same member functions