SlideShare a Scribd company logo
1 of 25
CLASSES AND
OBJECTS IN C++
by
Mrs. B.Arulmozhi, M.Sc., B.Ed., M.Phil., SET., NET.,
Assistant Professor and Head, Department of Computer
Science and Applications
D.K.M. College for Women, Vellore
SYNPOSIS
• Class
• Syntax
• Data Abstraction
• Example
• Creating Object
• Defining member function inside and
outside
• Memory allocation of object
• Example
CLASSES
It is a user defined data type.
A class is a way to bind the data and its
associated functions together.
Example:Class student
{
char name[30];
int rollno,m1,m2,tot;
public:void calculate();
};
Specifying class:
It has 2 parts.
1. class declaration
2. class function definitions.
Syntax:
Class class-name
{
private: variable-declaration; function-declaration;
public: variable-declaration; function-declaration;
protected: variable-declaration; function-declaration;
}
Example
class student
{
private:
char name[20];
int roll,m1,m2,tot;
protected:
void accept();
void compute();
void display();
public:
student();
void execute();
};
Rules
• class is a keyword
• Body of the class enclosed within braces and
terminated by ;
• Body contains declarations of variables and functions
• It contain 3 access specifiers.(visibility labels)
• private, public and protected
• private members accessed only with in the class.
• public members accessed outside of the class also.
• protected members accessed the members of the
inherted classes only
Data abstraction
• The binding of data and functions together into a
single entity is referred to as encapsulation.
• Data hiding is key features of OOPs.
• It can be lead using access specifiers.
•
Private Accessible only its own members
and certain special functions called
as friend functions.
Protected Accessible by members of inherited
classes.
Public Access allowed by other members
in addition to class members and
objects.
Data members and Member functions
• Members of the classes are data or functions.
• Variables are declared inside the class is called
data members. In the above ex: name,regno,
m1,m2 and tot are data members.
• Functions are declared inside the class is called
member functions.
• In above ex:accept(), compute(), display(),
execute() and student() are member functions.
CREATING OBJECTS
• Object is the instance variable of type class.
• Ex: student stud;
• Here stud is the object of user defined type student
class.
• We access the declared data type of student is lead
using object stud.
• ACCESSING CLASS MEMBERS
• Objectname.functionname();
• Ex: stud.execute();
• Dot operators is used to access the class
members.
Ex: using class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
void getdata()
{
a=5;b=6;
sum=a+b;
}
};
void main()
{
add a;//creating object
a.getdata();//access function
cout<<“Sum=”<<s.sum;
}
Output:
Sum =15
Ex: defining method outside of the
class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
void getdata();
};
void add::getdata()//outside
{
a=5;b=6;
sum=a+b;
}
void main()
{
add a;//creating object
a.getdata();//access function
cout<<“Sum=”<<s.sum;
}
Output:
Sum =15
Here add:: tells the compiler
that the function belongs to
the class name add.
Characteristics of member functions
• several different classes can use the same
function name. The membership label will
resolve the problem.
• Member function can access the private data of
a class. Non-member function cannot access it.
• A member function can call another member
function directly.(nesting of member function)
• Member function can receive the arguments of
a valid c++ data type. Object can also be
passed as arguments.
• A member function can return any valid data
type and also objects.
• A member function can be of static type
Memory allocation of Members
• Only one copy of memory allocated for member
functions
• For every objects, separate memory is allocated for
data members
• Ex: add a1,a2;
• 6 bytes for each objects a1 and a2.
• a1 object a2 object methods
a:
b:
Sum:
a:
b:
Sum:
getdata()
{
}
Example program- member function
handle arguments
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c;quantity=q;price=p;
}
void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<q
uanity;
cout<<“nCode=“<<price;
}
};
void main()
{
product p;
p.assign_data(101,200,12.5);
p.display();
}
Static data members
A data member of a class is qualified as static.
Rules:
• Initialized to zero when the first object of
the class is created.
• No other Initialization is allowed.
• Only one copy of member variable is
created. It is shared by all the other objects
of its class type.
• Its scope or visibility is within the class but
its life time is the lifetime of the program
Example program static data members
#include<iostream.h>
class sample
{
int a,b,sum;
static int count;
public:
void accept()
{
cout<<“Enter the value….”;
cin>>a>>b;
sum=a+b;
count++;
}
void display()
{
cout<<“n the sum of 2 no“<<sum;
cout<<“nthis is addition “<<count;
}
};
//static member initialized
out side of the class
int sample.count=0;
void main()
{
sample p1,p2,p3;
p1.accept();
p1.display();
p2.accept();
p2.display();
p3.accept();
p3.display();
}
Output:
Enter the values… 2 3
The sum of two numbers 5
This is addition 1
Enter the values… 5 3
The sum of two numbers 8
This is addition 2
Enter the values… 2 5
The sum of two numbers 7
This is addition 3
Rules:
Static member count is
initialized only once
of all the objects of
that class.
It is initialized outside of
the class.
Memory allocation of static
Members
• Only one copy of memory allocated static members of
all the classes
• In the above example p1,p2 and p3 objects
• p1 object p2 object p3 object
• static data members
a:2
b:3
Sum:5
a:5
b:3
Sum8:
a:2
b:5
Sum:7
count:3
Example program- array of objects
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c;quantity=q;price=p;
}
void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<q
uanity;
cout<<“nCode=“<<price;
}
} p[3];
void main()
{
p[0].assign_data(101,200,12.5);
p[0].display();
}
Array of objects
P[0]
Code:
Quantity
Price
P[1]
Code:
Quantity
Price
P[2]
Code:
Quantity
Price
Identify the errors ( Page no 164)
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c, quantity=q,price=p’;
}
void display();
};
Void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<qua
nity;
cout<<“nCode=“<<price;
}
} p[3];
void main()
{
p[0].assign_data(101,200,12.5);
p[0].display();
}
Solution:
void assign_data(int c,int q,float p)
{
code=c; quantity=q;price=p;
}
void product::display()
{
…..
}
Ex: defining method outside of the
class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
protected: void getdata();
};
void add::getdata()
{
a=b=0;
sum=a+b;
}
void main()
{
a=5;
b=6;
add a1;
a1.getdata();
cout<<“Sum=”<<sum;
}
Solution:
Protected member cannot be accessed
outside of the class.
getdata();
Private members cannot be accessed
outside of the class.
a=5
Sum is the public members of class add.
But it is accessed only object of that
class.
a1.sum;
Example program static data members
#include<iostream.h>
class sample
{
int a,b,sum;
static int count;
public:
void accept()
{
cout<<“Enter the value….”;
cin>>a>>b;
sum=a+b;
count++;
}
void display()
{
cout<<“n the sum of 2 no“<<sum;
cout<<“nthis is addition “<<count;
}
};
int sample.count=0;
void main()
{
sample p1,p2,p3;
p1.accept();
p1.display();
cout<<p1.count;//error
}

More Related Content

Similar to classes & objects.ppt

Similar to classes & objects.ppt (20)

Oop concepts
Oop conceptsOop concepts
Oop concepts
 
ccc
cccccc
ccc
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
class c++
class c++class c++
class c++
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
class and objects
class and objectsclass and objects
class and objects
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 

Recently uploaded

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 

Recently uploaded (20)

CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 

classes & objects.ppt

  • 1. CLASSES AND OBJECTS IN C++ by Mrs. B.Arulmozhi, M.Sc., B.Ed., M.Phil., SET., NET., Assistant Professor and Head, Department of Computer Science and Applications D.K.M. College for Women, Vellore
  • 2. SYNPOSIS • Class • Syntax • Data Abstraction • Example • Creating Object • Defining member function inside and outside • Memory allocation of object • Example
  • 3. CLASSES It is a user defined data type. A class is a way to bind the data and its associated functions together. Example:Class student { char name[30]; int rollno,m1,m2,tot; public:void calculate(); };
  • 4. Specifying class: It has 2 parts. 1. class declaration 2. class function definitions. Syntax: Class class-name { private: variable-declaration; function-declaration; public: variable-declaration; function-declaration; protected: variable-declaration; function-declaration; }
  • 5. Example class student { private: char name[20]; int roll,m1,m2,tot; protected: void accept(); void compute(); void display(); public: student(); void execute(); };
  • 6. Rules • class is a keyword • Body of the class enclosed within braces and terminated by ; • Body contains declarations of variables and functions • It contain 3 access specifiers.(visibility labels) • private, public and protected • private members accessed only with in the class. • public members accessed outside of the class also. • protected members accessed the members of the inherted classes only
  • 7. Data abstraction • The binding of data and functions together into a single entity is referred to as encapsulation. • Data hiding is key features of OOPs. • It can be lead using access specifiers. • Private Accessible only its own members and certain special functions called as friend functions. Protected Accessible by members of inherited classes. Public Access allowed by other members in addition to class members and objects.
  • 8. Data members and Member functions • Members of the classes are data or functions. • Variables are declared inside the class is called data members. In the above ex: name,regno, m1,m2 and tot are data members. • Functions are declared inside the class is called member functions. • In above ex:accept(), compute(), display(), execute() and student() are member functions.
  • 9. CREATING OBJECTS • Object is the instance variable of type class. • Ex: student stud; • Here stud is the object of user defined type student class. • We access the declared data type of student is lead using object stud. • ACCESSING CLASS MEMBERS • Objectname.functionname(); • Ex: stud.execute(); • Dot operators is used to access the class members.
  • 10. Ex: using class #include <iostream.h> Class add { private: int a,b; public:int sum; void getdata() { a=5;b=6; sum=a+b; } }; void main() { add a;//creating object a.getdata();//access function cout<<“Sum=”<<s.sum; } Output: Sum =15
  • 11. Ex: defining method outside of the class #include <iostream.h> Class add { private: int a,b; public:int sum; void getdata(); }; void add::getdata()//outside { a=5;b=6; sum=a+b; } void main() { add a;//creating object a.getdata();//access function cout<<“Sum=”<<s.sum; } Output: Sum =15 Here add:: tells the compiler that the function belongs to the class name add.
  • 12. Characteristics of member functions • several different classes can use the same function name. The membership label will resolve the problem. • Member function can access the private data of a class. Non-member function cannot access it. • A member function can call another member function directly.(nesting of member function) • Member function can receive the arguments of a valid c++ data type. Object can also be passed as arguments. • A member function can return any valid data type and also objects. • A member function can be of static type
  • 13. Memory allocation of Members • Only one copy of memory allocated for member functions • For every objects, separate memory is allocated for data members • Ex: add a1,a2; • 6 bytes for each objects a1 and a2. • a1 object a2 object methods a: b: Sum: a: b: Sum: getdata() { }
  • 14. Example program- member function handle arguments #include<iostream.h> class product { int code,quantity; float price; public: void assign_data(int c,int q,float p) { code=c;quantity=q;price=p; } void display() { cout<<“nCode=“<<code; cout<<“nQuantity=“<<q uanity; cout<<“nCode=“<<price; } }; void main() { product p; p.assign_data(101,200,12.5); p.display(); }
  • 15. Static data members A data member of a class is qualified as static. Rules: • Initialized to zero when the first object of the class is created. • No other Initialization is allowed. • Only one copy of member variable is created. It is shared by all the other objects of its class type. • Its scope or visibility is within the class but its life time is the lifetime of the program
  • 16. Example program static data members #include<iostream.h> class sample { int a,b,sum; static int count; public: void accept() { cout<<“Enter the value….”; cin>>a>>b; sum=a+b; count++; } void display() { cout<<“n the sum of 2 no“<<sum; cout<<“nthis is addition “<<count; } }; //static member initialized out side of the class int sample.count=0; void main() { sample p1,p2,p3; p1.accept(); p1.display(); p2.accept(); p2.display(); p3.accept(); p3.display(); }
  • 17. Output: Enter the values… 2 3 The sum of two numbers 5 This is addition 1 Enter the values… 5 3 The sum of two numbers 8 This is addition 2 Enter the values… 2 5 The sum of two numbers 7 This is addition 3 Rules: Static member count is initialized only once of all the objects of that class. It is initialized outside of the class.
  • 18. Memory allocation of static Members • Only one copy of memory allocated static members of all the classes • In the above example p1,p2 and p3 objects • p1 object p2 object p3 object • static data members a:2 b:3 Sum:5 a:5 b:3 Sum8: a:2 b:5 Sum:7 count:3
  • 19. Example program- array of objects #include<iostream.h> class product { int code,quantity; float price; public: void assign_data(int c,int q,float p) { code=c;quantity=q;price=p; } void display() { cout<<“nCode=“<<code; cout<<“nQuantity=“<<q uanity; cout<<“nCode=“<<price; } } p[3]; void main() { p[0].assign_data(101,200,12.5); p[0].display(); }
  • 21. Identify the errors ( Page no 164) #include<iostream.h> class product { int code,quantity; float price; public: void assign_data(int c,int q,float p) { code=c, quantity=q,price=p’; } void display(); }; Void display() { cout<<“nCode=“<<code; cout<<“nQuantity=“<<qua nity; cout<<“nCode=“<<price; } } p[3]; void main() { p[0].assign_data(101,200,12.5); p[0].display(); }
  • 22. Solution: void assign_data(int c,int q,float p) { code=c; quantity=q;price=p; } void product::display() { ….. }
  • 23. Ex: defining method outside of the class #include <iostream.h> Class add { private: int a,b; public:int sum; protected: void getdata(); }; void add::getdata() { a=b=0; sum=a+b; } void main() { a=5; b=6; add a1; a1.getdata(); cout<<“Sum=”<<sum; }
  • 24. Solution: Protected member cannot be accessed outside of the class. getdata(); Private members cannot be accessed outside of the class. a=5 Sum is the public members of class add. But it is accessed only object of that class. a1.sum;
  • 25. Example program static data members #include<iostream.h> class sample { int a,b,sum; static int count; public: void accept() { cout<<“Enter the value….”; cin>>a>>b; sum=a+b; count++; } void display() { cout<<“n the sum of 2 no“<<sum; cout<<“nthis is addition “<<count; } }; int sample.count=0; void main() { sample p1,p2,p3; p1.accept(); p1.display(); cout<<p1.count;//error }