Classes And
Objects
1.
2.
3.
4.
5.
6.

STRUCTURE
CLASS
CLASS DECLARATION
CLASS MEMBERS
ARRAY OF OBJECTS
PASSING OBJECTS TO FUNCTION

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
StructureName
 Example

Student ram;
Student s[5];

Compiled By: Kamal Acharya

variableName;
variableName[10];
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();

Compiled By: Kamal Acharya

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();
}
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);
Compiled By: Kamal Acharya

getch();
}
void display(struct student s1)
{
cout<<"Your Name is
"<<s1.name<<endl;
cout<<"Your Roll Number is
"<<s1.roll;
}
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;
}; By: Kamal Acharya
Compiled
Creating Objects
Example
class item
{
int number;
float cost;
public:
void getdata(int a , float
b);
void putdata();
};
Compiled By: Kamal Acharya

item x; //single object
item x[5]; // array
 At the time of definition
also we can create
objects.
class item
{
………
………..
}x,z[20];
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;
}
Compiled By: Kamal Acharya

void putdata()
{
cout<<number;
cout<<cost;
}

};
Defining member function outside the class
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata();
};

Compiled By: Kamal Acharya

void item::getdata(int a,
float b)
{
number=a;
cost=b;
}
void item::putdata()
{
cout<<number;
cout<<cost;
}
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;
}
Compiled By: Kamal Acharya

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();
}
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();
}
Compiled By: Kamal Acharya

int item::largest()
{
if(n>m)
return n;
else
return m;
}
void main()
{
item x;
clrscr();
x.getdata(20,70);
x.putdata();
getch();
}
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

Compiled By: Kamal Acharya

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();
}
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;
}
};
Compiled By: Kamal Acharya

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();
}
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;
}
Compiled By: Kamal Acharya

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();
}
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;
}

Compiled By: Kamal Acharya

void main()
{
test t1,t2,t3;
clrscr();
t1.getdata(40);
t2.getdata(20);
t3.add(t1,t2);
getch();
}
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;
}
Compiled By: Kamal Acharya

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();
}
OUTPUT

Compiled By: Kamal Acharya

Classes and objects

  • 1.
    Classes And Objects 1. 2. 3. 4. 5. 6. STRUCTURE CLASS CLASS DECLARATION CLASSMEMBERS ARRAY OF OBJECTS PASSING OBJECTS TO FUNCTION Compiled By: Kamal Acharya
  • 2.
    STRUCTURE  It isthe collection of different types of data under the common name. Syntax: struct StructureName { dataType var1; dataType var2; ………………….; dataType varN; }; Compiled By: Kamal Acharya
  • 3.
    Example: struct Student { int Roll; charName[20]; }; Compiled By: Kamal Acharya
  • 4.
     Declaring Variables StructureName StructureName Example Student ram; Student s[5]; Compiled By: Kamal Acharya variableName; variableName[10];
  • 5.
    Intialization: StructureName VariableName ={val1, val2,.........,valN}; Example: Student Ram={11,”Ram”}; Compiled By: Kamal Acharya
  • 6.
    Processing Structure #include<iostream.h> #include<conio.h> struct student { introll; char name[20]; }; void main() { student s1; clrscr(); Compiled By: Kamal Acharya 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(); }
  • 7.
  • 8.
    Passing Structure toFunction #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); Compiled By: Kamal Acharya getch(); } void display(struct student s1) { cout<<"Your Name is "<<s1.name<<endl; cout<<"Your Roll Number is "<<s1.roll; }
  • 9.
  • 10.
    LIMITATION OF STRUCTURES Structdata 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
  • 11.
     Do notpermit 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
  • 12.
    CLASSES  C++ incorporatesall 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
  • 13.
    Class Declaration  Syntax: classclassName { private: variables declaration; functions declaration; public: variables declaration; functions declaration; }; By: Kamal Acharya Compiled
  • 14.
    Creating Objects Example class item { intnumber; float cost; public: void getdata(int a , float b); void putdata(); }; Compiled By: Kamal Acharya item x; //single object item x[5]; // array  At the time of definition also we can create objects. class item { ……… ……….. }x,z[20];
  • 15.
    Accessing Class Members Syntax: objectName.functionName(arguments); Example x.getdata(100,75.5); Compiled By: Kamal Acharya
  • 16.
    Defining Member Function MemberFunction 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
  • 17.
    Defining member functioninside the class class item { int number; float cost; public: void getdata(int a,float b) { number=a; cost=b; } Compiled By: Kamal Acharya void putdata() { cout<<number; cout<<cost; } };
  • 18.
    Defining member functionoutside the class class item { int number; float cost; public: void getdata(int a,float b); void putdata(); }; Compiled By: Kamal Acharya void item::getdata(int a, float b) { number=a; cost=b; } void item::putdata() { cout<<number; cout<<cost; }
  • 19.
    C++ program withclass #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; } Compiled By: Kamal Acharya 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(); }
  • 20.
  • 21.
    Making Outside FunctionInline Class item { ………. public: ………….. void getdata(int a, float b); }; inline void item :: getdata(int a, float b) { function body; } Compiled By: Kamal Acharya
  • 22.
    Nesting of memberfunction #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(); } Compiled By: Kamal Acharya int item::largest() { if(n>m) return n; else return m; } void main() { item x; clrscr(); x.getdata(20,70); x.putdata(); getch(); }
  • 23.
  • 24.
    Memory Allocation forObjects • 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
  • 25.
  • 26.
    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
  • 27.
    Program to showthe 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 Compiled By: Kamal Acharya 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(); }
  • 28.
  • 29.
    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
  • 30.
    Program to showthe 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; } }; Compiled By: Kamal Acharya 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(); }
  • 31.
  • 32.
    Remember It Won’tWork static void showcount() { cout<<code; //code is not static } Compiled By: Kamal Acharya
  • 33.
    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
  • 34.
    Program to demonstratethe 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; } Compiled By: Kamal Acharya 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(); }
  • 35.
  • 36.
    Passing objects tofunction 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
  • 37.
    Passing by value #include<iostream.h> #include<conio.h> classtest { 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; } Compiled By: Kamal Acharya void main() { test t1,t2,t3; clrscr(); t1.getdata(40); t2.getdata(20); t3.add(t1,t2); getch(); }
  • 38.
  • 39.
    Passing By Reference #include<iostream.h> #include<conio.h> classtest { 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; } Compiled By: Kamal Acharya 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(); }
  • 40.