0% found this document useful (0 votes)
31 views9 pages

Chapter 5

The document discusses the differences between structures and classes in C and C++, highlighting how C++ enhances structures with features like data hiding and member functions. It explains the concepts of encapsulation, static members, friend functions, and the use of pointers to class members. Additionally, it covers the limitations of local classes and their access to variables, emphasizing the importance of member access control in object-oriented programming.

Uploaded by

Pandu Ranga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views9 pages

Chapter 5

The document discusses the differences between structures and classes in C and C++, highlighting how C++ enhances structures with features like data hiding and member functions. It explains the concepts of encapsulation, static members, friend functions, and the use of pointers to class members. Additionally, it covers the limitations of local classes and their access to variables, emphasizing the importance of member access control in object-oriented programming.

Uploaded by

Pandu Ranga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

5.

Classes and Objects


The standard C does not allow the struct data type to be
treated like built-in types. For example, consider the following
structure:
struct complex
{
float x;
float y;
};
struct complex cl, c2, c3;
The complex numbers c1, c 2, and c 3 can easily be assigned
values using the dot operator, but we cannot add two
complex numbers or subtract one from the other. For
example,
c3 = cl + c2;
is illegal in C.
Another important limitation of C structures is that they do
not permit data hiding. Structure members can be directly
accessed by the structure variables by any function
anywhere in their scope. In other words, the structure
members are public members.

C++ supports all the features of structures as defined in C.


But C++ has expanded its capabilities further to suit its OOP
philosophy. It attempts to bring the user-defined types as
close as possible to the built-in data types, and also provides
a facility to hide the data which is one of the main principles
of OOP.

In C++, a structure can have both variables and functions as


members. It can also declare some of its members as
‘private’ so that they cannot be accessed directly by the
external functions.
In C++, the structure names are stand-alone and can be
used like any other type names. In other words, the keyword
struct can be omitted in the declaration of structure
variables. This is not possible in C.

C++ incorporates all these extensions in another user-


defined type known as class. There is very little syntactical
difference between structures and classes in C++ and,
therefore, they can be used interchangeably with minor
modifications. Since class is a specially introduced data type
in C++, most of the C++ programmers tend to use the
structures for holding only data, and classes to hold both the
data and functions.

The only difference between a structure and a class in C++ is


that, by default, the members of a class are private, while, by
default, the members of a structure are public.

The class body contains the declaration of variables and


functions. These functions and variables are collectively
called class members.

The class members that have been declared as private can


be accessed only from within the class. On the other hand,
public members can be accessed from outside the class also.

By default, the members of a class are private. If both the


labels are missing, then, by default, all the members are
private. Such a class is completely hidden from the outside
world and does not serve any purpose.

The variables declared inside the class are known as data


members and the functions are known as member functions.
Only the member functions can have access to the private
data members and private functions. However, the public
members (both functions and data) can be accessed from
outside the class. The binding of data and functions together
into a single class-type variable is referred to as
encapsulation.

The data members are usually declared as private and the


member functions as public.

The declaration of an object is similar to that of a variable of


any basic type. The necessary memory space is allocated to
an object at this stage. Note that class specification, like a
structure, provides only a template and does not create any
memory space for the objects.

Remember, a member function can be invoked only by using


an object (of the same class).

Similarly, the statement


x.number = 100;
is also illegal. Although x is an object of the type item to
which number belongs, the number (declared private) can
be accessed only through a member function and not by the
object directly.

When a function is defined inside a class, it is treated as an


inline function. Therefore, all the restrictions and limitations
that apply to an
Inline function are also applicable here. Normally, only small
functions are defined inside the class definition.

We can define a member function outside the class definition


and still make it inline by just using the qualifier inline in the
header line of function definition.
Example:
inline void item :: getdata(int a, float b) // definition
{
number = a;
cost = b;
}

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.

We have stated that the memory space for objects is


allocated when they are declared and not when the class is
specified. This statement is only partly true. Actually, the
member functions are created and placed in the memory
space only once when they are defined as a part of a class
specification. Since all the objects belonging to that class use
the same member functions, no separate space is allocated
for member functions when the objects are created. Only
space for member variables is allocated separately for each
object.

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. A static member variable has certain special
characteristics. These are:
• 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.
Static variables are normally used to maintain values
common to the entire class. For example, a static data
member can be used as a counter that records the
occurrences of all the objects.

int item :: count; // definition of static data member


Note that the type and scope of each static member variable
must be defined outside the class definition. This is necessary
because the static data members are stored separately
rather than as a part of an object. Since they are associated
with the class itself rather than with any class object, they
are also known as class variables.

Static variables are like non-inline member functions as they


are declared in a class declaration and defined in the source
file. While defining a static variable, some initial value can
also be assigned to the variable. For instance, the following
definition gives count the initial value 10.
int item :: count = 10;

Like static member variable, we can also have static


member functions. A member function that is declared static
has the following
properties:
• A static function can have access to only other static
members (functions or variables) declared in the same class.
• A static member function can be called using the class
name (instead of its objects) as follows:
class-name :: function-name;

An object can also be passed as an argument to a


nonmember function. However, such functions 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.

We have been emphasizing throughout this chapter that the


private members cannot be accessed from outside the class.
That is, a nonmember function cannot have an access to the
private data of a class. However, there could be a situation
where we would like two classes to share a particular
function. For example, consider a case where two classes,
manager and scientist, have been defined. We would like
to use a function income_tax()to operate on the objects of
both these classes. In such situations, C++ allows the
common function to be made friendly with both the classes,
thereby allowing the function to have access to the private
data of these classes. Such a function need not be a member
of any of these classes.

To make an outside function “friendly” to a class, we have to


simply declare this function as a friend of the class as shown
below:
class ABC
{
....
....
public:
....
....
friend void xyz(void); // declaration
};

The function declaration should be preceded by the keyword


friend. The function is defined elsewhere in the program like
a normal C++ function. A friend function, although not a
member function, has full access rights to the private
members of the class.

A friend function possesses certain special characteristics:


• It is not in the scope of the class to which it has been
declared as friend.
• Since it is not in the scope of the class, it cannot be called
using the object of that class.
• It can be invoked like a normal function without the help of
any object.
• Unlike member functions, it cannot access the member
names directly and has to use an object name and dot
membership operator with each member name.(e.g. A.x).
• It can be declared either in the public or the private part of
a class without affecting its meaning.
• Usually, it has the objects as arguments.

Member functions of one class can be friend functions of


another class. In such cases, they are defined using the
scope resolution Operator.

We can also declare all the member functions of one class as


the friend functions of another class. In such cases, the class
is called a friend class.

Forward declaration.

If a member function does not alter any data in the class,


then we may declare it as a const member function as
follows:
void mul(int, int) const;
double get_balance() const;
The qualifier const is appended to the function prototypes (in
both declaration and definition).

It is possible to take the address of a member of a class and


assign it to a pointer. The address of a member can be
obtained by applying the operator & to a “fully qualified”
class member name. A class member pointer can be declared
using the operator ::* with the class name. For example,
given the class
class A
{
private:
int m;

public:
void show();
};
We can define a pointer to the member m as follows:
int A ::* ip = &A :: m;

The ip pointer created thus acts like a class member in that it


must be invoked with a class object. In the statement above,
the phrase A::* means “pointer-to-member of A class”. The
phrase &A::m means the “address of the m member of A
class”.

The pointer ip can now be used to access the member m


inside member functions (or friend functions). Let us assume
that a is an object of A declared in a member function. We
can access m using the pointer ip as follows:
cout << a.*ip; // display
cout << a.m; // same as above

The dereferencing operator ->* is used to access a member


when we use pointers to both the object and the member.
The dereferencing operator.* is used when the object itself is
used with the member pointer. Note that *ip is used like a
member name.

We can also design pointers to member functions which,


then, can be invoked using the dereferencing operators in the
main as shown below:
(object-name .* pointer-to-member function) (10);
(pointer-to-object ->* pointer-to-member function) (10)
Classes can be defined and used inside a function or a block.
Such classes are called local classes.

Local classes can use global variables (declared above the


function) and static variables declared inside the function but
cannot use automatic local variables. The global variables
should be used with the scope operator (::).
There are some restrictions in constructing local classes.
They cannot have static data members and member
functions must be defined inside the local classes.

You might also like