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
8.
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
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.
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.
21.
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.
26.
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.
28.
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.
44. Shows how to use a common friend to exchange the
private values of two classes . The finction is called by
reference
45.
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.
47.
48.
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.