SlideShare a Scribd company logo
Classes and objects
5.1 C structures
• Provide method for packing different data
types together
• Handling logically related data items
• User defined data type with template that
serve to define its data properties.
• We can create variable of that type using built
in type declarations.
Example of struct
Student is the new data type and we declare variable like this
Member variable can be accessed by dot or period operator
• Structures can have arrays pointers and
structures itself as members.
• Limitations of structure
– C doesn’t allow struct datatype to be treated as
builtin data type.
– They don’t permit data hiding, can be accesed by
any function
5.2 Classes : extension of structures
• In C++ , UD data types are attempted to be treated to be
like built in data type, facility to hide data, inheritence-one
type can inherit charecteristics from other types.
• In, C++, structures can be variables& functions
• Keyword struct is omitted
• The difference between classes and structures in C++ is that
• Stuctures hold only data, and members are public-by
default
• Classes hold data and functions, members are private by
default.
5.3 specifying a class
• Class specification has 2 parts
– 1.class declaration-describes types and scope of its
members.
– 2.class function definition-describes how functions
are implemented.
The general form of class declaration is:
• Private members are accessed only within the
class, its default declaration
• Public members-outside the class also
• The variables declared inside the class are
known as data members and functions as
member function.
• Only member functions can have access to
private data members and private functions.
• The process of binding data and functions
together in a single class type variable is ref to
as encapsulation
APL-2-classes and objects.ppt data structures using c++
A simple class declaration example
Getdata(), putdata() can
only be used to assign
member variables number
and cost and display
respectively.
These functions provide
the only access to data
members from outside
class.
This means that data
cannot be accessed by
any other function that is
not a member of the class
ITEM.
Creating objects
• Once class is declared we can create variables of that type. Or
even more than one variables.
Object is an instance of class.
We can also create object when class is defined by
placing immediately after closing brace as structures.
Accessing the class members
• Format for calling a member function is
Example:
Similarly
• Variable declared as public can be accessed by
objects directly.
5.4 defining member functions
Member functions can be defined in two places
– 1.outside the class definition
– 2. inside the class definition
Outside the class definition
• They are very much like normal functions.
• Should have function header and body.
• Imp difference: member function incorporates a
membership ‘identity label,(tells compiler which
class the function belong to), in the header.
example
APL-2-classes and objects.ppt data structures using c++
Inside the class definition
• Another method of defining a member function is to
replace the function declaration by actual definition
inside the class.
When a function
is in a class its
treated as inline
function.
Only small
functions are
defined inside the
class definition.
5.5 C++ program with class(class implementation)
5.6 Making an outside function inline
• We can still make member function inline by
using inline in the header line of function
definition.
5.7 Nesting of member functions
• We know that member function of a class can
be called only by an object of that class using
dot operator.
• Exception: It can also be called by using its
name inside another member function of
same class.
– This is known as nesting of member functions.
APL-2-classes and objects.ppt data structures using c++
5.8 private member function
• A private member function can only be called
by another function that is a member of its
class.
• Even an object cannot invoke a private
function using the dot operator.
If S1 is an object of sample then
Is illegal. However function read() can be called by function
update() to update the value of m
5.9 Arrays within a class
• Arrays can used as member variables in a
class.
The array a[] can
be used in the
member functions
like any other
variable.
We can perform
any operations on
it.
5.10 Memory allocation for objects
• Member functions are created and placed in
memory space only once when they are
defined as a part of class specification.
• No separate memory for members functions
as all objects belonging to same class use
same mem.fns.
• Separate memory locations for objects are
required as member variables will hold
different values for different objects.
APL-2-classes and objects.ppt data structures using c++
5.11 Static data members
• A static member variable has certain special charecteristics:
They are used to maintain same values common to
entire class.
Eg: counter that records the occurrences of all objects.
Static variable are like inline mem.fns as they as
declared in class declaration and defined in source file.
While defining , it can be assigned initial value as well.
APL-2-classes and objects.ppt data structures using c++
5.12 static member function
• A member function which is declared as static
has following properties:
– It can have access to only other static
members(fn/var) declared in the same class.
– Can be called using class name(instead of its
objects )
Example
5.13 Arrays of objects
• Arrays of variables that are of type class are
called arrays of objects.
5.14 Objects as function
arguments
• An object may be used as function argument
in two ways:
– A copy of entire object is passes to the function.
(pass by value)-only copy
– Only the address of the object is transferred to
the function.(pass by reference)-here fn works
directly on the actual objects used in the call. So
changes will affect actual object- efficient
Example
APL-2-classes and objects.ppt data structures using c++
An object can also be passed as argument to a non member
function.
Such fns can have access to the public member functions
only through the objects passed as arguments to it.
These functions cannot have access to the private data
members.
5.15 Friendly functions
• Wkt private functions cannot be accesed from
outside the class.
• In certain situation, C++ allows the common
function to be made friendly with both classes ,
thereby allowing the function to have access to all
the private data of these classes.
• We simply declare this function as friend of the
class .
• Eg:income tax() for managers and scientist class.
APL-2-classes and objects.ppt data structures using c++
APL-2-classes and objects.ppt data structures using c++
APL-2-classes and objects.ppt data structures using c++
Friend function
APL-2-classes and objects.ppt data structures using c++
Using friend function to add data objects of two
different classes
APL-2-classes and objects.ppt data structures using c++
Shows how to use a common friend to exchange the
private values of two classes . The finction is called by
reference
APL-2-classes and objects.ppt data structures using c++
5.16 returning objects
• A function can not only receive objects as
arguments but also can return them.
• The program shows how an object can be
created within the function and returned to
another function.
APL-2-classes and objects.ppt data structures using c++
APL-2-classes and objects.ppt data structures using c++
5.17 const member functions
• If a member function does not alter any data
in the class , then we may declare it as a const
member function.
The qualifier const is appended to function
prototypes both in declaration and definition.
The compiler will generate error message if such
functions try to alter data values.
5.18 POINTERS TO MEMBERS
Dereferencing operators
5.19 local classes

More Related Content

PPT
static member and static member fumctions.ppt
PPTX
object oriented programming-classes and objects.pptx
PPTX
Classes and objects
PPTX
Class and objects
PDF
Introduction to C++ Class & Objects. Book Notes
PPTX
Classes and objects in c++
PPTX
PPTX
Advanced Topics on Database - Unit-2 AU17
static member and static member fumctions.ppt
object oriented programming-classes and objects.pptx
Classes and objects
Class and objects
Introduction to C++ Class & Objects. Book Notes
Classes and objects in c++
Advanced Topics on Database - Unit-2 AU17

Similar to APL-2-classes and objects.ppt data structures using c++ (20)

PPTX
class and object in c++.pptx
PPT
Encapsulation
PPTX
PPTX
OOP Unit 2 - Classes and Object
DOC
My c++
PPT
C++ Programming Course
PPT
Ccourse 140618093931-phpapp02
PDF
Class and object
PPT
Classes in C++ computer language presentation.ppt
PDF
Classes-and-Objects-in-C++.pdf
PPTX
Class and object
PPTX
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPTX
Object Oriented Programming Using C++
PPT
Object Oriented Programming In .Net
PPTX
full defination of final opp.pptx
PPTX
C++ training
PDF
L1-Introduction to OOPs concepts.pdf
PPT
4 Classes & Objects
class and object in c++.pptx
Encapsulation
OOP Unit 2 - Classes and Object
My c++
C++ Programming Course
Ccourse 140618093931-phpapp02
Class and object
Classes in C++ computer language presentation.ppt
Classes-and-Objects-in-C++.pdf
Class and object
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
classandobjectunit2-150824133722-lva1-app6891.ppt
Object Oriented Programming Using C++
Object Oriented Programming In .Net
full defination of final opp.pptx
C++ training
L1-Introduction to OOPs concepts.pdf
4 Classes & Objects
Ad

More from ProfLSrividya (7)

PPTX
multiple access techniques in wireless mobile communication
PPTX
evolution of mobile communication from 1g-5g.pptx
PPTX
module_1.pptx wireless mobile communication
PPTX
evolution of wireless mobile communication
PPTX
Evolution of wireless mobile communication-1a.pptx
PPTX
introduction to wireless mobile communications.pptx
PDF
ieee-code-of-ethics.pdf
multiple access techniques in wireless mobile communication
evolution of mobile communication from 1g-5g.pptx
module_1.pptx wireless mobile communication
evolution of wireless mobile communication
Evolution of wireless mobile communication-1a.pptx
introduction to wireless mobile communications.pptx
ieee-code-of-ethics.pdf
Ad

Recently uploaded (20)

PDF
Well-logging-methods_new................
PPTX
Welding lecture in detail for understanding
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
PPT on Performance Review to get promotions
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Construction Project Organization Group 2.pptx
Well-logging-methods_new................
Welding lecture in detail for understanding
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
UNIT 4 Total Quality Management .pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Sustainable Sites - Green Building Construction
R24 SURVEYING LAB MANUAL for civil enggi
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Digital Logic Computer Design lecture notes
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT on Performance Review to get promotions
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Lecture Notes Electrical Wiring System Components
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Construction Project Organization Group 2.pptx

APL-2-classes and objects.ppt data structures using c++

  • 2. 5.1 C structures • Provide method for packing different data types together • Handling logically related data items • User defined data type with template that serve to define its data properties. • We can create variable of that type using built in type declarations.
  • 3. Example of struct Student is the new data type and we declare variable like this Member variable can be accessed by dot or period operator
  • 4. • Structures can have arrays pointers and structures itself as members. • Limitations of structure – C doesn’t allow struct datatype to be treated as builtin data type. – They don’t permit data hiding, can be accesed by any function
  • 5. 5.2 Classes : extension of structures • In C++ , UD data types are attempted to be treated to be like built in data type, facility to hide data, inheritence-one type can inherit charecteristics from other types. • In, C++, structures can be variables& functions • Keyword struct is omitted • The difference between classes and structures in C++ is that • Stuctures hold only data, and members are public-by default • Classes hold data and functions, members are private by default.
  • 6. 5.3 specifying a class • Class specification has 2 parts – 1.class declaration-describes types and scope of its members. – 2.class function definition-describes how functions are implemented. The general form of class declaration is:
  • 7. • Private members are accessed only within the class, its default declaration • Public members-outside the class also • The variables declared inside the class are known as data members and functions as member function. • Only member functions can have access to private data members and private functions. • The process of binding data and functions together in a single class type variable is ref to as encapsulation
  • 9. A simple class declaration example Getdata(), putdata() can only be used to assign member variables number and cost and display respectively. These functions provide the only access to data members from outside class. This means that data cannot be accessed by any other function that is not a member of the class ITEM.
  • 10. Creating objects • Once class is declared we can create variables of that type. Or even more than one variables. Object is an instance of class. We can also create object when class is defined by placing immediately after closing brace as structures.
  • 11. Accessing the class members • Format for calling a member function is Example: Similarly
  • 12. • Variable declared as public can be accessed by objects directly.
  • 13. 5.4 defining member functions Member functions can be defined in two places – 1.outside the class definition – 2. inside the class definition
  • 14. Outside the class definition • They are very much like normal functions. • Should have function header and body. • Imp difference: member function incorporates a membership ‘identity label,(tells compiler which class the function belong to), in the header.
  • 17. Inside the class definition • Another method of defining a member function is to replace the function declaration by actual definition inside the class. When a function is in a class its treated as inline function. Only small functions are defined inside the class definition.
  • 18. 5.5 C++ program with class(class implementation)
  • 19. 5.6 Making an outside function inline • We can still make member function inline by using inline in the header line of function definition.
  • 20. 5.7 Nesting of member functions • We know that member function of a class can be called only by an object of that class using dot operator. • Exception: It can also be called by using its name inside another member function of same class. – This is known as nesting of member functions.
  • 22. 5.8 private member function • A private member function can only be called by another function that is a member of its class. • Even an object cannot invoke a private function using the dot operator.
  • 23. If S1 is an object of sample then Is illegal. However function read() can be called by function update() to update the value of m
  • 24. 5.9 Arrays within a class • Arrays can used as member variables in a class. The array a[] can be used in the member functions like any other variable. We can perform any operations on it.
  • 25. 5.10 Memory allocation for objects • Member functions are created and placed in memory space only once when they are defined as a part of class specification. • No separate memory for members functions as all objects belonging to same class use same mem.fns. • Separate memory locations for objects are required as member variables will hold different values for different objects.
  • 27. 5.11 Static data members • A static member variable has certain special charecteristics: They are used to maintain same values common to entire class. Eg: counter that records the occurrences of all objects. Static variable are like inline mem.fns as they as declared in class declaration and defined in source file. While defining , it can be assigned initial value as well.
  • 29. 5.12 static member function • A member function which is declared as static has following properties: – It can have access to only other static members(fn/var) declared in the same class. – Can be called using class name(instead of its objects )
  • 31. 5.13 Arrays of objects • Arrays of variables that are of type class are called arrays of objects.
  • 32. 5.14 Objects as function arguments • An object may be used as function argument in two ways: – A copy of entire object is passes to the function. (pass by value)-only copy – Only the address of the object is transferred to the function.(pass by reference)-here fn works directly on the actual objects used in the call. So changes will affect actual object- efficient
  • 35. An object can also be passed as argument to a non member function. Such fns can have access to the public member functions only through the objects passed as arguments to it. These functions cannot have access to the private data members.
  • 36. 5.15 Friendly functions • Wkt private functions cannot be accesed from outside the class. • In certain situation, C++ allows the common function to be made friendly with both classes , thereby allowing the function to have access to all the private data of these classes. • We simply declare this function as friend of the class . • Eg:income tax() for managers and scientist class.
  • 42. Using friend function to add data objects of two different classes
  • 44. Shows how to use a common friend to exchange the private values of two classes . The finction is called by reference
  • 46. 5.16 returning objects • A function can not only receive objects as arguments but also can return them. • The program shows how an object can be created within the function and returned to another function.
  • 49. 5.17 const member functions • If a member function does not alter any data in the class , then we may declare it as a const member function. The qualifier const is appended to function prototypes both in declaration and definition. The compiler will generate error message if such functions try to alter data values.
  • 50. 5.18 POINTERS TO MEMBERS