Classes & Objects
Chap 5
3/13/2016 1By:-Gourav Kottawar
Contents
5.1 A Sample C++ Program with class
5.2 Access specifies
5.3 Defining Member Functions
5.4 Making an Outside Function Inline
5.5 Nesting of Member Functions
5.6 Private Member Functions
5.7 Arrays within a Class
5.8 Memory Allocation for Objects
5.9 Static Data Members, Static Member
5.10 Functions, Arrays of Objects
5.11 Object as Function Arguments
5.12 Friend Functions, Returning Objects,
5.13 Const member functions
5.14 Pointer to Members, Local Classes
5.15 Object composition & delegation
3/13/2016 2By:-Gourav Kottawar
Declaring Class
 Syntax –
class class_name
{
private :
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
}
3/13/2016 3By:-Gourav Kottawar
Data hiding in classes
3/13/2016 4By:-Gourav Kottawar
Simple class program
Creating objects
Accessing class members
 Access specifies
private
public
protector
default - private
 5.3 Defining Member Functions
3/13/2016 5By:-Gourav Kottawar
5.4 Making an Outside Function Inline
class item
{
......
......
public:
void getdata(int a,float b);
};
inline void item :: getdata(int a,float b)
{
number=a;
cost=b;
}
3/13/2016 6By:-Gourav Kottawar
5.5 Nesting of Member Functions
#include<iostream.h>
#include<conio.h>
class data
{
int rollno,maths,science;
int avg();
public: void getdata();
void putdata();
};
main()
{
clrscr();
data stud[5];
for(int i=0;i<5;i++)
stud[i].getdata();
}
void data::getdata()
{
cout<<"Please enter rollno:";
cin>>rollno;
cout<<"Please enter maths marks:";
cin>>maths;
cout<<"Please enter science marks:";
cin>>science;
putdata();
}
int data::avg()
{
int a; a=(maths+science)/2;
return a;
}
void data::putdata()
{
cout<<"Average is :"<<avg()<<endl;
}
3/13/2016 7By:-Gourav Kottawar
5.7 Arrays within a Class
const int size=10;
class array
{
int a[size];
public:
void setval(void);
void display(void);
};
3/13/2016 8By:-Gourav Kottawar
Ex-
#include<iostream>
#include<string>
using namespace std;
const int val=50;
class ITEM
{
private:
int item_code[val];
int item_price[val];
int count;
public:
void initiliaze();
void get_item();
void display_item();
void display_sum();
void remove();
};
void ITEM::initiliaze()
{
count=0;
}
void ITEM::get_item()
{
cout<<"Enter the Item code == "<<endl;
cin>>item_code[count];
cout<<"Enter the Item cost == "<<endl;
cin>>item_price[count];
count++;
}
void ITEM::display_sum()
{
int sum=0;
for(int i=0; i<count;i++)
{
sum=sum + item_price[i];
}
cout<<"The Total Value Of The Cost Is ==
"<<sum<<endl;
}
3/13/2016 9By:-Gourav Kottawar
void ITEM::display_item()
{
cout<<"nCode Pricen";
for(int k=0;k<count;k++)
{
cout<<"n"<<item_code[k];
cout<<" "<<item_price[k];
}
}
void ITEM::remove()
{
int del;
cout<<"Enter the code you want to remove == ";
cin>>del;
for(int search=0; search<count; search++)
{
if(del == item_code[search])
{
item_price[search]=0;
item_code[search]=0;
}
}
}
3/13/2016 10By:-Gourav Kottawar
int main()
{
ITEM order;
order.initiliaze();
int x;
do
{
cout<<"nnYou have the following opton";
cout<<"nEnter the Appropriate number";
cout<<"nnPress 1 for ADD AN ITEMS";
cout<<"nnPress 2 for DISPLAY TOTAL
VALUE";
cout<<"nnPress 3 for DELETE AN
ITEM";
cout<<"nnPress 4 for DISPLAY ALL
ITEMS";
cout<<"nnPress 5 for QUIT";
cout<<"nEnter The Desired Number ==
n";
cin>>x;
switch(x)
{
case 1:
{
order.get_item();
break;
}
case 2:
{
order.display_sum();
break;
}
case 3:
{
order.remove();
break;
}
case 4:
{
order.display_item();
break;
}
3/13/2016 11By:-Gourav Kottawar
case 5:
break;
default: cout<<"Incorrect option Please
Press the right number == ";
}
}while(x!=5);
getchar();
return 0;
}
3/13/2016 12By:-Gourav Kottawar
5.8 Memory Allocation for Objects
common for all objects
member function 1
member function 2 memory created when
function defined
Object 1 object 2 object 3
Member varible 1 member variable 2 member
variable 1
Member variable 2 member variable 2 member variable 2
memory created
when objects defined
3/13/2016 13By:-Gourav Kottawar
5.9 Static Data Members, Static Member
 A data member of a class can be
qualified as static.
 The properties of a static member
variable are similar to that of a C static
variable.
 It is initialized to zero when the first
object of its class is created. No other
initialization is permitted.
 Only one copy of that member is created
for the entire class and is shared by all
the objects of that class, no matter how
many objects are created.
 It is visible only within the class, but its
lifetime is the entire program.
3/13/2016 14By:-Gourav Kottawar
#include
using namespace std;
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count ++;
}
void getcount(void)
{
cout << "Count: ";
cout << count <<"n";
}
};
int item :: count;
int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout << "After reading
data"<<"n";
a.getcount();
b.getcount();
c.getcount();
return 0;
}
3/13/2016 15By:-Gourav Kottawar
#include
using namespace std;
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count ++;
}
void getcount(void)
{
cout << "Count: ";
cout << count <<"n";
}
};
int item :: count;
int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout << "After reading
data"<<"n";
a.getcount();
b.getcount();
c.getcount();
return 0;
}
The output of
the program
would be:
Count: 0
Count: 0
Count: 0
After reading
data
Count: 3
Count: 3
Count: 3
3/13/2016 16By:-Gourav Kottawar
Sharing of a static data
member
3/13/2016 17By:-Gourav Kottawar
Static member function
 A member function that is declared
static has following properties
1. A static function can have access to
only other static members (functions
or variables) declared in the same
class.
2. A static member function can be
called using the class name (instead
of its objects ) as follows :
class – name :: function – name;3/13/2016 18By:-Gourav Kottawar
3/13/2016 19
class Something
{
private:
static int s_nValue;
public:
static int GetValue() { return s_nValue; }
};
int Something::s_nValue = 1; // initializer
int main()
{
std::cout << Something::GetValue() <<
std::endl;
}
By:-Gourav Kottawar
3/13/2016 20
class IDGenerator
{
private:
static int s_nNextID;
public:
static int GetNextID() { return
s_nNextID++; }
};
// We'll start generating IDs at 1
int IDGenerator::s_nNextID = 1;
int main()
{
for (int i=0; i < 5; i++)
cout << "The next ID is: " <<
IDGenerator::GetNextID() << endl;
return 0;
}
The next ID is: 1
The next ID is: 2
The next ID is: 3
The next ID is: 4
The next ID is: 5
By:-Gourav Kottawar
3/13/2016 21
class IDGenerator
{
private:
static int s_nNextID;
public:
static int GetNextID() { return s_nNextID++; }
};
// We'll start generating IDs at 1
int IDGenerator::s_nNextID = 1;
int main()
{
for (int i=0; i < 5; i++)
cout << "The next ID is: " << IDGenerator::GetNextID() <<
endl;
return 0;
}
By:-Gourav Kottawar
Array of object
3/13/2016 22By:-Gourav Kottawar
5.11 Object as Function Arguments
 Two ways
1. A copy of the entire object is passed
to the function.
i.e. pass by value
2. Only the address of the object is
transferred to the function
i.e. pass by reference
3/13/2016 23By:-Gourav Kottawar
3/13/2016 24By:-Gourav Kottawar
3/13/2016 25
#include <iostream>
using namespace std;
class Complex
{
private: int real;
int imag;
public:
void Read()
{
cout<<"Enter real and imaginary number
respectively:"<<endl; cin>>real>>imag;
}
void Add(Complex comp1,Complex comp2)
{
real=comp1.real+comp2.real;
/* Here, real represents the real data of object c3 because this function is
called using code c3.add(c1,c2); */
imag=comp1.imag+comp2.imag;
/* Here, imag represents the imag data of object c3 because this function
is called using code c3.add(c1,c2); */
}
By:-Gourav Kottawar
3/13/2016 26
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
};
int main()
{
Complex c1,c2,c3;
c1.Read();
c2.Read();
c3.Add(c1,c2);
c3.Display();
return 0;
}
By:-Gourav Kottawar
Returning Object from Function
3/13/2016 27By:-Gourav Kottawar
3/13/2016 28
#include <iostream>
using namespace std;
class Complex
{
private: int real;
int imag;
public: void Read()
{
cout<<"Enter real and imaginary number respectively:"<<endl;
cin>>real>>imag;
}
Complex Add(Complex comp2)
{
Complex temp;
temp.real=real+comp2.real;
/* Here, real represents the real data of object c1 because this
function is called using code c1.Add(c2) */
temp.imag=imag+comp2.imag;
/* Here, imag represents the imag data of object c1 because this
function is called using code c1.Add(c2) */
return temp; 0;
} By:-Gourav Kottawar
3/13/2016 29
}
void Display()
{
cout<<"Sum="<<real<<"+"<<ima
g<<"i"; }
};
int main()
{
Complex c1,c2,c3;
c1.Read();
c2.Read();
c3=c1.Add(c2);
c3.Display();
return 0;
}
By:-Gourav Kottawar
5.12 Friend function
 Need
a data is declared as private inside a
class, then it is not accessible from
outside the class.
A function that is not a member or an
external class will not be able to access
the private data.
A programmer may have a situation
where he or she would need to access
private data from non-member functions
and external classes. For handling such
cases, the concept of Friend functions is
a useful tool.
3/13/2016 30By:-Gourav Kottawar
5.12 Friend function
What is a Friend Function?
 A friend function is used for accessing
the non-public members of a class.
 A class can allow non-
member functions and other classes to
access its own private data, by making
them friends.
 Thus, a friend function is an ordinary
function or a member of another class.
3/13/2016 31By:-Gourav Kottawar
5.12 Friend function
 General syntax
Class ABC
{
…..
…..
public :
…..
…..
Friend void func1(void);
};
3/13/2016 32By:-Gourav Kottawar
5.12 Friend function
Special Characteristics
 The keyword friend is placed only in the function declaration
of the friend function and not in the function definition.
 It is not in the scope of the class to which it has been declared
as friend.
 It is possible to declare a function as friend in any number of
classes.
 When a class is declared as a friend, the friend class has access
to the private data of the class that made this a friend.
 A friend function, even though it is not a member function,
would have the rights to access the private members of the
class.
 It is possible to declare the friend function as either private or
public without affecting meaning.
 The function can be invoked without the use of an object.
 It has object as argument.
3/13/2016 33By:-Gourav Kottawar
3/13/2016 34
#include <iostream>
using namespace std;
class exforsys
{
private:
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int compute(exforsys e1);
//Friend Function
Declaration with keyword friend and
with the object of class exforsys to
which it is friend passed to it
};
int compute(exforsys e1)
{
//Friend Function Definition
which has access to private data
return int(e1.a+e1.b)-5;
}
void main()
{
exforsys e;
e.test();
cout << "The result is:" <<
compute(e);
//Calling of Friend
Function with object as argument.
}
By:-Gourav Kottawar
Constant Member Functions
 Declaring a member function with
the const keyword specifies that the
function is a "read-only" function that
does not modify the object for which it is
called.
 A constantmember function cannot
modify any non-static data members or
call any member functions that aren't
constant.
 The const keyword is required in both the
declaration and the definition.
3/13/2016 35By:-Gourav Kottawar
Constant Member Functions –
EXclass Date
{
public:
Date( int mn, int dy, int yr );
int getMonth() const; // A
read-only function
void setMonth( int mn ); // A
write function; can't be const
private:
int month;
};
int Date::getMonth() const
{
return month; // Doesn't
modify anything
}
3/13/2016 36
void Date::setMonth( int mn )
{
month = mn; // Modifies data
member
}
int main()
{
Date MyDate( 7, 4, 1998 );
const Date BirthDate( 1, 18, 1953
); MyDate.setMonth( 4 ); // Okay
BirthDate.getMonth(); // Okay
BirthDate.setMonth( 4 ); // C2662
Error
}
By:-Gourav Kottawar
5.14 Pointers to members
 Pointers to members allow you to refer to nonstatic
members of class objects.
 You cannot use a pointer to member to point to a static
class member because the address of a static member is
not associated with any particular object.
 To point to a static class member, you must use a
normal pointer.
 You can use pointers to member functions in the same
manner as pointers to functions.
 You can compare pointers to member functions, assign
values to them, and use them to call member functions.
 Note - a member function does not have the same type
as a nonmember function that has the same number
and type of arguments and the same return type.
3/13/2016 37By:-Gourav Kottawar
5.14 Pointers to members - Ex
#include <iostream>
using namespace std;
class X
{
public:
int a;
void f(int b)
{
cout << "The value of b is
"<< b << endl;
}
};
3/13/2016 38
int main()
{
// declare pointer to data member
int X::*ptiptr = &X::a;
// declare a pointer to member
function
void (X::* ptfptr) (int) = &X::f;
// create an object of class type X
X xobject;
// initialize data member
xobject.*ptiptr = 10;
cout << "The value of a is " <<
xobject.*ptiptr << endl;
// call member function
(xobject.*ptfptr) (20);
}
By:-Gourav Kottawar
5.14 Local Classes
 A local class is declared within a function
definition.
 Declarations in a local class can only use type
names, enumerations, static variables from the
enclosing scope, as well as external variables and
functions.
 Member functions of a local class have to be
defined within their class definition, if they are
defined at all.
 As a result, member functions of a local class are
inline functions. Like all member functions, those
defined within the scope of a local class do not need
the keyword inline.
3/13/2016 39By:-Gourav Kottawar
3/13/2016 40
int x; // global variable
void f() // function definition
{
static int y; // static variable y can be used by local class
int x; // auto variable x cannot be used by local class
extern int g(); // extern function g can be used by local class
class local // local class
{
int g() { return x; } // error, local variable x
// cannot be used by g
int h() { return y; } // valid,static variable y
int k() { return ::x; } // valid, global x
int l() { return g(); } // valid, extern function g
};
}
int main()
{
local* z; // error: the class local is not
visible
// ...}
By:-Gourav Kottawar
3/13/2016 41
A local class cannot have static data members.
void f()
{
class local
{
int f(); // error, local class has noninline
// member function
int g() {return 0;} // valid, inline member
function
static int a; // error, static is not allowed for //
local class
int b; // valid, nonstatic variable
};
} // . . .
By:-Gourav Kottawar

More Related Content

PPTX
SCHEDULING ALGORITHMS
PPTX
Polymorphism in Python
PPTX
Algorithm
PPTX
Introduction to Operating Systems
PPTX
Pointers in c++
PPTX
Pointers
PDF
Pseudocode & flowchart examples
PPTX
stack & queue
SCHEDULING ALGORITHMS
Polymorphism in Python
Algorithm
Introduction to Operating Systems
Pointers in c++
Pointers
Pseudocode & flowchart examples
stack & queue

What's hot (20)

PPT
Arithmetic operator
PDF
C++ OOPS Concept
PPTX
Process state in OS
PPTX
Evolution of os
PPTX
Loops in Python
PDF
Python tuple
PPT
File access methods.54
PPT
EULER AND FERMAT THEOREM
PPT
PPT
Python Control structures
PDF
Control statements
PPTX
Recursive Function
PPT
stacks in algorithems and data structure
PPTX
Strings in C language
PPTX
PPT
Structure of C++ - R.D.Sivakumar
PPTX
heap Sort Algorithm
PPTX
Overloading vs Overriding.pptx
PDF
Polymorphism In Java
PPTX
File handling in Python
Arithmetic operator
C++ OOPS Concept
Process state in OS
Evolution of os
Loops in Python
Python tuple
File access methods.54
EULER AND FERMAT THEOREM
Python Control structures
Control statements
Recursive Function
stacks in algorithems and data structure
Strings in C language
Structure of C++ - R.D.Sivakumar
heap Sort Algorithm
Overloading vs Overriding.pptx
Polymorphism In Java
File handling in Python
Ad

Viewers also liked (8)

PPT
Basics of cpp
DOCX
C++ file
PPTX
Classes And Objects
PPTX
class and objects
PPTX
Classes, objects in JAVA
PPT
Oops ppt
PPT
Object Oriented Programming Concepts
Basics of cpp
C++ file
Classes And Objects
class and objects
Classes, objects in JAVA
Oops ppt
Object Oriented Programming Concepts
Ad

Similar to classes & objects in cpp (20)

PPTX
class c++
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PDF
Class object
PPT
Classes in C++ computer language presentation.ppt
PPT
classes & objects.ppt
PPTX
Class and object
PPSX
Arre tari mano bhosko ka pikina tari ma no piko
PPT
Unit vi(dsc++)
PPT
APL-2-classes and objects.ppt
PPTX
Classes and objects1
PPT
APL-2-classes and objects.ppt data structures using c++
PDF
Classes and objects
PDF
Classes and objects
PDF
Introduction to C++ Class & Objects. Book Notes
PPTX
Data members and member functions
PPTX
classes and objects in C++
PPT
static member and static member fumctions.ppt
PPTX
Classes and objects
PPT
4 Classes & Objects
PDF
Chapter 7 C++ As OOP
class c++
classandobjectunit2-150824133722-lva1-app6891.ppt
Class object
Classes in C++ computer language presentation.ppt
classes & objects.ppt
Class and object
Arre tari mano bhosko ka pikina tari ma no piko
Unit vi(dsc++)
APL-2-classes and objects.ppt
Classes and objects1
APL-2-classes and objects.ppt data structures using c++
Classes and objects
Classes and objects
Introduction to C++ Class & Objects. Book Notes
Data members and member functions
classes and objects in C++
static member and static member fumctions.ppt
Classes and objects
4 Classes & Objects
Chapter 7 C++ As OOP

More from gourav kottawar (20)

PPTX
operator overloading & type conversion in cpp
PPTX
constructor & destructor in cpp
PPTX
expression in cpp
PPTX
basics of c++
PPT
working file handling in cpp overview
PPT
pointers, virtual functions and polymorphisms in c++ || in cpp
PPTX
exception handling in cpp
PPT
cpp input & output system basics
PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
constructor & destructor in cpp
PPTX
basics of c++
PPTX
classes & objects in cpp overview
PPTX
expression in cpp
PPT
SQL || overview and detailed information about Sql
PPT
SQL querys in detail || Sql query slides
PPT
Rrelational algebra in dbms overview
PPT
overview of database concept
PPT
Relational Model in dbms & sql database
PPTX
DBMS information in detail || Dbms (lab) ppt
PPTX
security and privacy in dbms and in sql database
operator overloading & type conversion in cpp
constructor & destructor in cpp
expression in cpp
basics of c++
working file handling in cpp overview
pointers, virtual functions and polymorphisms in c++ || in cpp
exception handling in cpp
cpp input & output system basics
operator overloading & type conversion in cpp over view || c++
constructor & destructor in cpp
basics of c++
classes & objects in cpp overview
expression in cpp
SQL || overview and detailed information about Sql
SQL querys in detail || Sql query slides
Rrelational algebra in dbms overview
overview of database concept
Relational Model in dbms & sql database
DBMS information in detail || Dbms (lab) ppt
security and privacy in dbms and in sql database

Recently uploaded (20)

PDF
Climate and Adaptation MCQs class 7 from chatgpt
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
PowerPoint for Climate Change by T.T.pdf
PDF
Journal of Dental Science - UDMY (2020).pdf
PDF
semiconductor packaging in vlsi design fab
PDF
CRP102_SAGALASSOS_Final_Projects_2025.pdf
PPTX
INSTRUMENT AND INSTRUMENTATION PRESENTATION
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
1.Salivary gland disease.pdf 3.Bleeding and Clotting Disorders.pdf important
PDF
My India Quiz Book_20210205121199924.pdf
PDF
plant tissues class 6-7 mcqs chatgpt.pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
Module on health assessment of CHN. pptx
PDF
HVAC Specification 2024 according to central public works department
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
MICROPARA INTRODUCTION XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
PDF
International_Financial_Reporting_Standa.pdf
Climate and Adaptation MCQs class 7 from chatgpt
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PowerPoint for Climate Change by T.T.pdf
Journal of Dental Science - UDMY (2020).pdf
semiconductor packaging in vlsi design fab
CRP102_SAGALASSOS_Final_Projects_2025.pdf
INSTRUMENT AND INSTRUMENTATION PRESENTATION
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
1.Salivary gland disease.pdf 3.Bleeding and Clotting Disorders.pdf important
My India Quiz Book_20210205121199924.pdf
plant tissues class 6-7 mcqs chatgpt.pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Module on health assessment of CHN. pptx
HVAC Specification 2024 according to central public works department
Literature_Review_methods_ BRACU_MKT426 course material
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
MICROPARA INTRODUCTION XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
International_Financial_Reporting_Standa.pdf

classes & objects in cpp

  • 1. Classes & Objects Chap 5 3/13/2016 1By:-Gourav Kottawar
  • 2. Contents 5.1 A Sample C++ Program with class 5.2 Access specifies 5.3 Defining Member Functions 5.4 Making an Outside Function Inline 5.5 Nesting of Member Functions 5.6 Private Member Functions 5.7 Arrays within a Class 5.8 Memory Allocation for Objects 5.9 Static Data Members, Static Member 5.10 Functions, Arrays of Objects 5.11 Object as Function Arguments 5.12 Friend Functions, Returning Objects, 5.13 Const member functions 5.14 Pointer to Members, Local Classes 5.15 Object composition & delegation 3/13/2016 2By:-Gourav Kottawar
  • 3. Declaring Class  Syntax – class class_name { private : variable declaration; function declaration; public: variable declaration; function declaration; } 3/13/2016 3By:-Gourav Kottawar
  • 4. Data hiding in classes 3/13/2016 4By:-Gourav Kottawar
  • 5. Simple class program Creating objects Accessing class members  Access specifies private public protector default - private  5.3 Defining Member Functions 3/13/2016 5By:-Gourav Kottawar
  • 6. 5.4 Making an Outside Function Inline class item { ...... ...... public: void getdata(int a,float b); }; inline void item :: getdata(int a,float b) { number=a; cost=b; } 3/13/2016 6By:-Gourav Kottawar
  • 7. 5.5 Nesting of Member Functions #include<iostream.h> #include<conio.h> class data { int rollno,maths,science; int avg(); public: void getdata(); void putdata(); }; main() { clrscr(); data stud[5]; for(int i=0;i<5;i++) stud[i].getdata(); } void data::getdata() { cout<<"Please enter rollno:"; cin>>rollno; cout<<"Please enter maths marks:"; cin>>maths; cout<<"Please enter science marks:"; cin>>science; putdata(); } int data::avg() { int a; a=(maths+science)/2; return a; } void data::putdata() { cout<<"Average is :"<<avg()<<endl; } 3/13/2016 7By:-Gourav Kottawar
  • 8. 5.7 Arrays within a Class const int size=10; class array { int a[size]; public: void setval(void); void display(void); }; 3/13/2016 8By:-Gourav Kottawar
  • 9. Ex- #include<iostream> #include<string> using namespace std; const int val=50; class ITEM { private: int item_code[val]; int item_price[val]; int count; public: void initiliaze(); void get_item(); void display_item(); void display_sum(); void remove(); }; void ITEM::initiliaze() { count=0; } void ITEM::get_item() { cout<<"Enter the Item code == "<<endl; cin>>item_code[count]; cout<<"Enter the Item cost == "<<endl; cin>>item_price[count]; count++; } void ITEM::display_sum() { int sum=0; for(int i=0; i<count;i++) { sum=sum + item_price[i]; } cout<<"The Total Value Of The Cost Is == "<<sum<<endl; } 3/13/2016 9By:-Gourav Kottawar
  • 10. void ITEM::display_item() { cout<<"nCode Pricen"; for(int k=0;k<count;k++) { cout<<"n"<<item_code[k]; cout<<" "<<item_price[k]; } } void ITEM::remove() { int del; cout<<"Enter the code you want to remove == "; cin>>del; for(int search=0; search<count; search++) { if(del == item_code[search]) { item_price[search]=0; item_code[search]=0; } } } 3/13/2016 10By:-Gourav Kottawar
  • 11. int main() { ITEM order; order.initiliaze(); int x; do { cout<<"nnYou have the following opton"; cout<<"nEnter the Appropriate number"; cout<<"nnPress 1 for ADD AN ITEMS"; cout<<"nnPress 2 for DISPLAY TOTAL VALUE"; cout<<"nnPress 3 for DELETE AN ITEM"; cout<<"nnPress 4 for DISPLAY ALL ITEMS"; cout<<"nnPress 5 for QUIT"; cout<<"nEnter The Desired Number == n"; cin>>x; switch(x) { case 1: { order.get_item(); break; } case 2: { order.display_sum(); break; } case 3: { order.remove(); break; } case 4: { order.display_item(); break; } 3/13/2016 11By:-Gourav Kottawar
  • 12. case 5: break; default: cout<<"Incorrect option Please Press the right number == "; } }while(x!=5); getchar(); return 0; } 3/13/2016 12By:-Gourav Kottawar
  • 13. 5.8 Memory Allocation for Objects common for all objects member function 1 member function 2 memory created when function defined Object 1 object 2 object 3 Member varible 1 member variable 2 member variable 1 Member variable 2 member variable 2 member variable 2 memory created when objects defined 3/13/2016 13By:-Gourav Kottawar
  • 14. 5.9 Static Data Members, Static Member  A data member of a class can be qualified as static.  The properties of a static member variable are similar to that of a C static variable.  It is initialized to zero when the first object of its class is created. No other initialization is permitted.  Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.  It is visible only within the class, but its lifetime is the entire program. 3/13/2016 14By:-Gourav Kottawar
  • 15. #include using namespace std; class item { static int count; int number; public: void getdata(int a) { number = a; count ++; } void getcount(void) { cout << "Count: "; cout << count <<"n"; } }; int item :: count; int main() { item a,b,c; a.getcount(); b.getcount(); c.getcount(); a.getdata(100); b.getdata(200); c.getdata(300); cout << "After reading data"<<"n"; a.getcount(); b.getcount(); c.getcount(); return 0; } 3/13/2016 15By:-Gourav Kottawar
  • 16. #include using namespace std; class item { static int count; int number; public: void getdata(int a) { number = a; count ++; } void getcount(void) { cout << "Count: "; cout << count <<"n"; } }; int item :: count; int main() { item a,b,c; a.getcount(); b.getcount(); c.getcount(); a.getdata(100); b.getdata(200); c.getdata(300); cout << "After reading data"<<"n"; a.getcount(); b.getcount(); c.getcount(); return 0; } The output of the program would be: Count: 0 Count: 0 Count: 0 After reading data Count: 3 Count: 3 Count: 3 3/13/2016 16By:-Gourav Kottawar
  • 17. Sharing of a static data member 3/13/2016 17By:-Gourav Kottawar
  • 18. Static member function  A member function that is declared static has following properties 1. A static function can have access to only other static members (functions or variables) declared in the same class. 2. A static member function can be called using the class name (instead of its objects ) as follows : class – name :: function – name;3/13/2016 18By:-Gourav Kottawar
  • 19. 3/13/2016 19 class Something { private: static int s_nValue; public: static int GetValue() { return s_nValue; } }; int Something::s_nValue = 1; // initializer int main() { std::cout << Something::GetValue() << std::endl; } By:-Gourav Kottawar
  • 20. 3/13/2016 20 class IDGenerator { private: static int s_nNextID; public: static int GetNextID() { return s_nNextID++; } }; // We'll start generating IDs at 1 int IDGenerator::s_nNextID = 1; int main() { for (int i=0; i < 5; i++) cout << "The next ID is: " << IDGenerator::GetNextID() << endl; return 0; } The next ID is: 1 The next ID is: 2 The next ID is: 3 The next ID is: 4 The next ID is: 5 By:-Gourav Kottawar
  • 21. 3/13/2016 21 class IDGenerator { private: static int s_nNextID; public: static int GetNextID() { return s_nNextID++; } }; // We'll start generating IDs at 1 int IDGenerator::s_nNextID = 1; int main() { for (int i=0; i < 5; i++) cout << "The next ID is: " << IDGenerator::GetNextID() << endl; return 0; } By:-Gourav Kottawar
  • 22. Array of object 3/13/2016 22By:-Gourav Kottawar
  • 23. 5.11 Object as Function Arguments  Two ways 1. A copy of the entire object is passed to the function. i.e. pass by value 2. Only the address of the object is transferred to the function i.e. pass by reference 3/13/2016 23By:-Gourav Kottawar
  • 25. 3/13/2016 25 #include <iostream> using namespace std; class Complex { private: int real; int imag; public: void Read() { cout<<"Enter real and imaginary number respectively:"<<endl; cin>>real>>imag; } void Add(Complex comp1,Complex comp2) { real=comp1.real+comp2.real; /* Here, real represents the real data of object c3 because this function is called using code c3.add(c1,c2); */ imag=comp1.imag+comp2.imag; /* Here, imag represents the imag data of object c3 because this function is called using code c3.add(c1,c2); */ } By:-Gourav Kottawar
  • 26. 3/13/2016 26 void Display() { cout<<"Sum="<<real<<"+"<<imag<<"i"; } }; int main() { Complex c1,c2,c3; c1.Read(); c2.Read(); c3.Add(c1,c2); c3.Display(); return 0; } By:-Gourav Kottawar
  • 27. Returning Object from Function 3/13/2016 27By:-Gourav Kottawar
  • 28. 3/13/2016 28 #include <iostream> using namespace std; class Complex { private: int real; int imag; public: void Read() { cout<<"Enter real and imaginary number respectively:"<<endl; cin>>real>>imag; } Complex Add(Complex comp2) { Complex temp; temp.real=real+comp2.real; /* Here, real represents the real data of object c1 because this function is called using code c1.Add(c2) */ temp.imag=imag+comp2.imag; /* Here, imag represents the imag data of object c1 because this function is called using code c1.Add(c2) */ return temp; 0; } By:-Gourav Kottawar
  • 29. 3/13/2016 29 } void Display() { cout<<"Sum="<<real<<"+"<<ima g<<"i"; } }; int main() { Complex c1,c2,c3; c1.Read(); c2.Read(); c3=c1.Add(c2); c3.Display(); return 0; } By:-Gourav Kottawar
  • 30. 5.12 Friend function  Need a data is declared as private inside a class, then it is not accessible from outside the class. A function that is not a member or an external class will not be able to access the private data. A programmer may have a situation where he or she would need to access private data from non-member functions and external classes. For handling such cases, the concept of Friend functions is a useful tool. 3/13/2016 30By:-Gourav Kottawar
  • 31. 5.12 Friend function What is a Friend Function?  A friend function is used for accessing the non-public members of a class.  A class can allow non- member functions and other classes to access its own private data, by making them friends.  Thus, a friend function is an ordinary function or a member of another class. 3/13/2016 31By:-Gourav Kottawar
  • 32. 5.12 Friend function  General syntax Class ABC { ….. ….. public : ….. ….. Friend void func1(void); }; 3/13/2016 32By:-Gourav Kottawar
  • 33. 5.12 Friend function Special Characteristics  The keyword friend is placed only in the function declaration of the friend function and not in the function definition.  It is not in the scope of the class to which it has been declared as friend.  It is possible to declare a function as friend in any number of classes.  When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend.  A friend function, even though it is not a member function, would have the rights to access the private members of the class.  It is possible to declare the friend function as either private or public without affecting meaning.  The function can be invoked without the use of an object.  It has object as argument. 3/13/2016 33By:-Gourav Kottawar
  • 34. 3/13/2016 34 #include <iostream> using namespace std; class exforsys { private: int a,b; public: void test() { a=100; b=200; } friend int compute(exforsys e1); //Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passed to it }; int compute(exforsys e1) { //Friend Function Definition which has access to private data return int(e1.a+e1.b)-5; } void main() { exforsys e; e.test(); cout << "The result is:" << compute(e); //Calling of Friend Function with object as argument. } By:-Gourav Kottawar
  • 35. Constant Member Functions  Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.  A constantmember function cannot modify any non-static data members or call any member functions that aren't constant.  The const keyword is required in both the declaration and the definition. 3/13/2016 35By:-Gourav Kottawar
  • 36. Constant Member Functions – EXclass Date { public: Date( int mn, int dy, int yr ); int getMonth() const; // A read-only function void setMonth( int mn ); // A write function; can't be const private: int month; }; int Date::getMonth() const { return month; // Doesn't modify anything } 3/13/2016 36 void Date::setMonth( int mn ) { month = mn; // Modifies data member } int main() { Date MyDate( 7, 4, 1998 ); const Date BirthDate( 1, 18, 1953 ); MyDate.setMonth( 4 ); // Okay BirthDate.getMonth(); // Okay BirthDate.setMonth( 4 ); // C2662 Error } By:-Gourav Kottawar
  • 37. 5.14 Pointers to members  Pointers to members allow you to refer to nonstatic members of class objects.  You cannot use a pointer to member to point to a static class member because the address of a static member is not associated with any particular object.  To point to a static class member, you must use a normal pointer.  You can use pointers to member functions in the same manner as pointers to functions.  You can compare pointers to member functions, assign values to them, and use them to call member functions.  Note - a member function does not have the same type as a nonmember function that has the same number and type of arguments and the same return type. 3/13/2016 37By:-Gourav Kottawar
  • 38. 5.14 Pointers to members - Ex #include <iostream> using namespace std; class X { public: int a; void f(int b) { cout << "The value of b is "<< b << endl; } }; 3/13/2016 38 int main() { // declare pointer to data member int X::*ptiptr = &X::a; // declare a pointer to member function void (X::* ptfptr) (int) = &X::f; // create an object of class type X X xobject; // initialize data member xobject.*ptiptr = 10; cout << "The value of a is " << xobject.*ptiptr << endl; // call member function (xobject.*ptfptr) (20); } By:-Gourav Kottawar
  • 39. 5.14 Local Classes  A local class is declared within a function definition.  Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions.  Member functions of a local class have to be defined within their class definition, if they are defined at all.  As a result, member functions of a local class are inline functions. Like all member functions, those defined within the scope of a local class do not need the keyword inline. 3/13/2016 39By:-Gourav Kottawar
  • 40. 3/13/2016 40 int x; // global variable void f() // function definition { static int y; // static variable y can be used by local class int x; // auto variable x cannot be used by local class extern int g(); // extern function g can be used by local class class local // local class { int g() { return x; } // error, local variable x // cannot be used by g int h() { return y; } // valid,static variable y int k() { return ::x; } // valid, global x int l() { return g(); } // valid, extern function g }; } int main() { local* z; // error: the class local is not visible // ...} By:-Gourav Kottawar
  • 41. 3/13/2016 41 A local class cannot have static data members. void f() { class local { int f(); // error, local class has noninline // member function int g() {return 0;} // valid, inline member function static int a; // error, static is not allowed for // local class int b; // valid, nonstatic variable }; } // . . . By:-Gourav Kottawar