Classes and Objects:-
Class:-
A class is a user defined data that contained data as well as function together.
or
A class is a way to bind the data and function together in a single unit.
or
A class is a group of objects that share common properties and relationships
Note:-
1. The variables and functions enclosed in a class are called data member and member
functions.
2. The variable of class are called objects or instance of a class .
3. Classes are basic user defined data type of object oriented programming language
4. The difference between a class and structure is that ,by default ,all the members of a
class are private while by default ,all the members of structure are public
Class Declaration:-
Class <classname>
{
private:
data members;
member functions;
protected:
data members;
member functions;
public:
data members ;
member functions:
};
Note:- The data members and member functions of class are grouped under
three sections.
1. private
2. protected
3. public
Explanation of private ,protected & public access modifier:-
private :-
• By private we mean that members can be accessed only from within the class i.e member
data can be accessed by the member function .
• The private data members are not accessible to the outside worls (i.e outside the class)
• By default members of a class are private.
• Private members are not inheritable.
Protected:-
• The members which are declared as protected ,can only be accessed by member functions
and friends function function of that class.
1
, • Protected members are similer to private members ,the only difference is that protected
members are inheritable .
Public:-
The members which are declared as public can be accessed from out side classes also.
Note:- By default ,the members of a class are private .if both the lables ,are missing,then by
difault ,all the members are private .Such a class is completely hidden from outsite worls.
Example:-
class Student
{
int rollno;
float marks;
public:
void getdata(int x,flot y);
void display(void);
};
Array of Objects:- An array of variables that are of the type Class .such variables are
called array of objects.
Example.
class employee
{
char name[50];
int age;
public:
void getdata(void);
void showdata(void);
};
The employee is a user defined data type and can be used to create objects that relate to
different categories of employees.
employee manager[4];
employee engineer[15];
employee workers[200];
Creating Objects:-
1. An object is an instance of class .
2. In general a class is a user defined data type .while an object is an instance of class
3. Once a class has been declared ,we can create variables of that type by using class name.
4. An object occupies memory space.
Let Student be the name of class
Student S1, S2;
or
Class Student
{
--------------
----------------
} S1,S2;
2
,Accessing Class Members:- After creating the objects there is a need to access the class
members .This can be done using ( . ) operator .
Syntax:
Objectname . Datamember
Objectname.memberfunction
As:
S1.getdata(129,100)
s1.display()
s1.rollno=129 // illegal statement
Note:- A variable declared public can be accessed by the objects directly.
//Example for public data members
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
class date
{
public:
int day;
int month;
int year;
};
date d1;
d1.day=26;
d1.month=6;
d1.year=2012
cout<<“ Date is=“<<d1.day<<“/” <<d1.months<<“/” <<d1.year<<endl;
getch();
}
Example 2:-Write a program to make use of simple arithmetic operations such as
addition,subtraction,multiplication division using the concept class.
#include <iostream.h>
#include<conio.h>
class calculator
{
private :
int x;
int y;
public :
void getdata();
{
cout<<“ Enter the two numbers:” << endl;
cin>> x >> y;
}
void display()
3
, {
cout<<“x=“<<x<<endl;
cout<<“y=“<<y<<endl;
}
void sum()
{
cout<<“ addition of x & y =“<<x+y<<endl;
}
void diff()
{
cout << “ diff of x & y =“ <<x-y<<endl;
}
void mul()
{
cout<<“ mul of x & y =“<<x*y<<endl;
}
void div()
{
cout<< “ div of x & y=“<< x/y<<endl;
}
};
Void main()
{
clrscr();
calculator C1;
c1.getdata();
c1.display();
c1.sum();
c1.diff();
c1.mul();
c1.div();
getch();
}
Class method definition:- The data members of a class must be declared within the body of
the class ,whereas member functions of the class can be defined in following ways:
1. Out side the class definition
2. Inside the class definitation
Member function outside the class definition:-
1. declare function prototype within the body of a class and then define it outside the body of class.
2. By using scope resolution operator ( : : ) we bind the function to the class.
Syntax:-
Return type classname :: functionname(argument declaration)
{
function body
}
4