classes and objects
TRANSCRIPT
1 . STRUCTURE 2 . CLASS 3 . CLASS DECLARATION 4 . CLASS MEMBERS 5 . ARRAY OF OBJECTS 6 . PASSING OBJECTS TO FUNCTION
Classes And Objects
Compiled By: Kamal Acharya
STRUCTURE
It is the collection of different types of data under the common name.
Syntax:
struct StructureName
{
dataType var1;
dataType var2;
………………….;
dataType varN;
};
Compiled By: Kamal Acharya
Example: struct Student
{
int Roll;
char Name[20];
} ;
Compiled By: Kamal Acharya
Declaring Variables
StructureName variableName;
StructureName variableName[10];
Example
Student ram;
Student s[5];
Compiled By: Kamal Acharya
Intialization:
StructureName VariableName ={ val1, val2,.........,valN};
Example:
Student Ram={11,”Ram”};
Compiled By: Kamal Acharya
Processing Structure
#include<iostream.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
};
void main()
{
student s1;
clrscr();
cout<<"Enter Roll Number"<<endl;
cin>>s1.roll;
cout<<"Enter Your Name"<<endl;
cin>>s1.name;
cout<<"Your Name is "<<s1.name<<endl;
cout<<"Your Roll Number is "<<s1.roll;
getch();
}
Compiled By: Kamal Acharya
Output
Compiled By: Kamal Acharya
Passing Structure to Function
#include<iostream.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
};
void display(struct student s);
void main()
{
student s1={12,"Sita"};
clrscr();
display(s1);
getch();
}
void display(struct student s1)
{
cout<<"Your Name is "<<s1.name<<endl;
cout<<"Your Roll Number is "<<s1.roll;
}
Compiled By: Kamal Acharya
Output
Compiled By: Kamal Acharya
LIMITATION OF STRUCTURES
Struct data type can’t be treated as the built in type.
Example:
struct complex
{
int real;
int img;
}c1,c2,c3;
c3= c1+c2; //Illegal
Compiled By: Kamal Acharya
Do not permit data hiding. Structure members can be directly accessed by structure variables by any function any where
Example
complex c1;
c1.real= 20;
c1.img=70;
Compiled By: Kamal Acharya
CLASSES
C++ incorporates all the features of structure while avoiding the drawbacks in new user defined data types called Class.
Classes can hold both data and the functions
Classes members are private by default.
Compiled By: Kamal Acharya
Class Declaration
Syntax:
class className
{
private:
variables declaration;
functions declaration;
public:
variables declaration;
functions declaration;
}; Compiled By: Kamal Acharya
Creating Objects
Example
class item
{
int number;
float cost;
public:
void getdata(int a , float b);
void putdata();
} ;
item x; //single object
item x[5]; // array
At the time of definition also we can create objects.
class item
{
………
………..
}x,z[20];
Compiled By: Kamal Acharya
Accessing Class Members
Syntax:
objectName.functionName(arguments);
Example
x.getdata(100,75.5);
Compiled By: Kamal Acharya
Defining Member Function
Member Function can be defined in two places:
1. Inside the Class
Are inline by default.
2. Outside the Class
Syntax:
returnType className::functionName(arguments)
{
function body;
}
Compiled By: Kamal Acharya
Defining member function inside the class
class item
{
int number;
float cost;
public:
void getdata(int a,float b)
{
number=a;
cost=b;
}
void putdata()
{
cout<<number;
cout<<cost;
}
};
Compiled By: Kamal Acharya
Defining member function outside the class
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata();
};
void item::getdata(int a, float b)
{
number=a;
cost=b;
}
void item::putdata()
{
cout<<number;
cout<<cost;
} Compiled By: Kamal Acharya
C++ program with class
#include<iostream.h>
#include<conio.h>
class item
{
int number;
float cost;
public:
void getdata(int a, float b);
void putdata()
{
cout<<"Number:"<<number<<endl;
cout<<"Cost:"<<cost<<endl;
}
};
void item::getdata(int a, float b)
{
number=a;
cost=b;
}
void main()
{
item x;
clrscr();
cout<<"Object x"<<endl;
x.getdata(10,2.9);
x.putdata();
item y;
cout<<"Object y"<<endl;
y.getdata(12,4.7);
y.putdata();
getch();
}
Compiled By: Kamal Acharya
Output
Compiled By: Kamal Acharya
Making Outside Function Inline
Class item
{
……….
public:
…………..
void getdata(int a, float b);
};
inline void item :: getdata(int a, float b)
{
function body;
}
Compiled By: Kamal Acharya
Nesting of member function
#include<iostream.h>
#include<conio.h>
class item
{
int n,m;
public:
void getdata(int a, int b);
int largest();
void putdata();
};
void item::getdata(int a, int b)
{
n=a;
m=b;
}
void item::putdata()
{
cout<<"The largest number is " <<largest();
}
int item::largest()
{
if(n>m)
return n;
else
return m;
}
void main()
{
item x;
clrscr();
x.getdata(20,70);
x.putdata();
getch();
}
Compiled By: Kamal Acharya
OUTPUT
Compiled By: Kamal Acharya
Memory Allocation for Objects
• Each objects has its own separate data items and memory are allocated when the objects are created.
• Member function are created and put in the memory only once when class are defined.
Compiled By: Kamal Acharya
Compiled By: Kamal Acharya
Static Data Members
It is initialized to zero when first object is created.
Only one copy is created for entire class.
It is visible within the class but its life time is the entire program.
Type and scope of each static member variable must be defined outside the class definition.
They are stored separately rather than as a part of the object.
Compiled By: Kamal Acharya
Program to show the static data members
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number=a;
count++;
}
void getcount()
{
cout<<"Count: "<<count<<endl;
}
};
int item::count; // REMEMBER THIS
void main()
{
item a,b,c;
clrscr();
cout<<"Before Reading Data" <<endl;
a.getcount();
b.getcount();
c.getcount();
a.getdata(10);
b.getdata(20);
c.getdata(30);
cout<<"After Reading Data" <<endl;
a.getcount();
b.getcount();
c.getcount();
getch();
}
Compiled By: Kamal Acharya
OUTPUT
Compiled By: Kamal Acharya
Static Member Function
It has access to only other static members(function or variables) declared in the same class.
To declare any function static just put the keyword static in front of the function defination.
static returnType functionName(arguments)
{
function body;
}
It can be called using the class name as follows:
className :: functionName(arguments);
Compiled By: Kamal Acharya
Program to show the work of static function
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
int code;
public:
void setcode()
{
code=++count;
}
void showcode()
{
cout<<"Object Number: "<<code<<endl;
}
static void showcount()
{
cout<<"Count: "<<count<<endl;
}
};
int item::count;
void main()
{
item t1,t2;
clrscr();
t1.setcode();
t2.setcode();
item::showcount();
item t3;
t3.setcode();
item::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}
Compiled By: Kamal Acharya
OUTPUT
Compiled By: Kamal Acharya
Remember It Won’t Work
static void showcount()
{
cout<<code; //code is not static
}
Compiled By: Kamal Acharya
Array of Objects
We can create the array of objects as:
className variable[x];
To access the individual element we have to take the help of the dot operator.
variable[i].function(arguments);
Compiled By: Kamal Acharya
Program to demonstrate the array of objects
#include<iostream.h>
#include<conio.h>
class employee
{
char name[30];
int age;
public:
void getdata();
void putdata();
};
void employee::getdata()
{
cout<<"Enter name:"<<endl;
cin>>name;
cout<<"Enter age:"<<endl;
cin>>age;
cout<<endl;
}
void employee::putdata()
{
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
}
void main()
{
employee manager[2];
int i;
clrscr();
for(i=0;i<2;i++)
{
cout<<"Details of manager"<<i+1<<endl;
manager[i].getdata();
}
cout<<endl;
for(i=0;i<2;i++)
{
cout<<endl<<"Manager"<<i+1<<endl;
manager[i].putdata();
}
getch();
}
Compiled By: Kamal Acharya
OUTPUT
Compiled By: Kamal Acharya
Passing objects to function
Two Ways to pass the objects to the function:
1. Passing by value
Entire object is passed is passed to the function.
2. Passing by reference
Only the reference to the object is passed.
Compiled By: Kamal Acharya
Passing by value
#include<iostream.h>
#include<conio.h>
class test
{
int a;
public:
void getdata(int x)
{
a=x;
}
void add(test, test);
};
void test::add(test x, test y)
{
cout<<"The sum of the values is: “ <<x.a+y.a<<endl;
}
void main()
{
test t1,t2,t3;
clrscr();
t1.getdata(40);
t2.getdata(20);
t3.add(t1,t2);
getch();
}
Compiled By: Kamal Acharya
OUTPUT
Compiled By: Kamal Acharya
Passing By Reference
#include<iostream.h>
#include<conio.h>
class test
{
int a;
public:
void getdata(int x)
{
a=x;
}
void putdata()
{
cout<<"a= "<<a<<endl;
}
void change(test &, test &);
};
void test::change(test &x, test &y)
{
int temp;
temp=x.a;
x.a=y.a;
y.a=temp;
}
void main()
{
test t1,t2,t3;
clrscr();
t1.getdata(40);
t2.getdata(20);
cout<<"Before Swapping"<<endl;
cout<<"In t1"<<endl;
t1.putdata();
cout<<"In t2"<<endl;
t2.putdata();
t3.change(t1,t2);
cout<<"After Swapping"<<endl;
cout<<"In t1"<<endl;
t1.putdata();
cout<<"In t2"<<endl;
t2.putdata();
getch();
}
Compiled By: Kamal Acharya
OUTPUT
Compiled By: Kamal Acharya