Class Temper
Class Temper
Class Temper
Classes
A class is the fundamental building block of object
oriented program
It contains a set of data items and functions that operate
on these data
A class definition of two parts
Class head
Class head is made up of class keyword followed by class
name of the class
Class body
The class body is the portion of the class definition enclosed
within pair of curly braces
Body consists of both data and functions.
….
Class body defines a scope which is commonly called
class scope
There are two keyword
Private
The private data members are accessed by the members
functions only
Public
The data members are accessed by the member function &
function out side the class
Class structure
example
Class student
{
Private:
Char name[20];
Int age;
Float per;
Public:
Void read();
Void print();
};
Data members
The data component of a class are called data
members
Data members of ex: student
Name, age, percentage
The data members cannot be initialized explicitly with
in class
Constructors are used to initialized the data members
of a class
Member functions
A member function can be declared two ways
Inside the class
Out side the class using scope resolution operator
Point to remember
A member function must be declared in public section
The member function belong to one class have no access
to the members class of another class
A member function can be overloaded
A member function cannot be overloaded the function
in other class
….
Syntax
Return_type funct_name(parameters);
Declaration outside
Return_type
class_name::member_function_name(parameter_list);
Example Members function inside the class
#include <iostream.h>
class student
{
private: int rollno;
char name[20];
float percentage;
void main()
{
student boy; //object creation
boy.getData();
boy.printData();
}
Example Members function outside the class
#include <iostream.h>
class student
{
private: int rollno;
char name[20];
float percentage;
public:void getdata();
void printdata();
};
void student ::getData (void)
{
cout <<"Enter the roll number"<<endl;
cin >> rollno;
cout <<"Enter the name"<<endl;
cin >> name;
cout <<"Enter the percentage"<<endl;
cin >> percentage;
}
….
void student :: printData(void)
{
cout <<"Roll number = "<<rollno<<endl;
cout <<"Name = "<<name <<endl;
cout <<"Percentage = "<<percentage<<endl;
}
void main()
{
student boy; //object creation
boy.getData();
boy.printData();
}
Member access
There are 3 member access specifies in c++
Private
The private members can be accessed only by the members
function of that class only
Public
Can be accessed any where in the class
Protected
In inheritance concept we use this for inheriting the base class
to deriver class
Class definition versus declaration
Class definition Class declaration
A class definition mad A class declaration can be
outside the main made inside or outside the
A class definition is a process class
of writing the actual class Its tells the compiler to store
body followed by head the amount of memory for
The class definition the objects
introduces new data type
It does not allocate any
memory space
Class objects
An object is a real world entity
A class object is the instance of the class
The memory is allocated for the objects
Class student s1,s2,s3,s4,s5[20];
Points to remember
Each objects has its own copy of data members
An object has a scope
An object has its lifetime
Pointer & references to objects can be declared
Member access operators
Dot operator(.)
S1.name;s1.age;
Pointer (->)
S2->name;s2->age;
Member function
Member function are usually declared in public
section
The class members function can be declared inside or
outside the class
There can be one copy of the members function & it is
shared by almost all the objects
The members function defined inside the class called
inline
Array of objects
Int a[20], char name[20], struct student s[20];
Class student s[20];
Example:
#include<iostream.h>
Class empl
{
char name[20];
int age;
public:
void getdata();
void putdata();
};
Void empl :: getdata()
{
cout<<“Enter name:”;
cin>>name;
cout<<“Enter age:”;
cin>>age;
}
Void empl::putdata()
{
cout<<“Name:”<<name<<“\n”;
cout<<“Age:<<age<<“\n”;
}
Void main()
{ empl e1[4];
for(int i=0;i<size;i++)
{
cout<<“\nEnter the Details of manager”<<i+1;
e1[i].getdata();
}
Cout<<“\n”;
for(i=0;i<size;i++)
{
cout<<“\nDetails of manager”<<i+1;
e1[i].putdata();
}
}
Pointers.
The pointer can be used in two ways
.
->
Example:-
#include<iostream.h>
#include<conio.h>
class name
{
char name[20];
public:
void get();
void put();
};
void name::get()
{
cout<<"Enter u r name\n";
cin>>name;
}
void name::put()
{
cout<<"U r name is "<<name;
}
void main()
{
class name *n; n->get(); n->put();
}
Difference between
Struct in c Class in c++
First begins with struct First begins with the keyword
keyword class
Only data members can be Both data members and
defined members functions can be
By default all data members declared
are public By default all data members &
Its defines new user defined members functions are
data type private
Its also defines new user
defined data type
Struct in c++
There is no difference between struct & class in c++
both use data members and members functions
Only by default in struct is public but in class is private
Example using struct
#include<iostream.h>
#include<conio.h>
struct name
{
char name[20];
int age;
public:
void get();
void put();
};
void name::get()
{
cout<<"Enter u r name\n";
cin>>name;
cout<<"Enter u r age\n"; cin>>age;
}
void name::put()
{
cout<<"U r name is "<<name<<" Age is "<<age;
}
void main()
{
struct name n;
clrscr();
n.get();
n.put();
getch();
}
Example using struct with pointer
#include<iostream.h>
#include<conio.h>
struct name
{
char name[20];
int age;
public:
void get();
void put();
};
void name::get()
{
cout<<"Enter u r name\n";
cin>>name;
cout<<"Enter u r age\n"; cin>>age;
}
void name::put()
{
cout<<"U r name is "<<name<<" Age is "<<age;
}
void main()
{
struct name *n;
clrscr();
n->get();
n->put();
getch();
}
Nested classes
Class within another class is called nested class
Example:-
#include<iostream.h>
#include<conio.h>
class outer
{
int age;
char name[20];
public:
void out_get();
void out_put();
class inner
{
char major[20];
float per;
public:
void in_get();
void in_put();
};
};
void outer::out_get()
{
cout<<"Enter u r name\n";
cin>>name;
cout<<"Enter u r age"<<endl;
cin>>age;
}
void outer::out_put()
{
cout<<"U r name is : "<<name<<" Age is : "<<age;
}
void outer::inner::in_get()
{
cout<<"Enter u r major subject"<<endl;
cin>>major;
cout<<"Enter u r percentage"<<endl;
cin>>per;
}
void outer::inner::in_put()
{
cout<<"Major subject is "<<major<<" & percentage is "<<per;
}
void main()
{
class outer o;
class outer::inner i;
clrscr();
o.out_get();
i.in_get();
o.out_put();
i.in_put();
getch();
}