0% found this document useful (0 votes)
35 views

Classes and Objects: C++ 6 Sem, A' Div 2018-19 Ms. Mouna M. Naravani

The document discusses classes and objects in C++. It covers limitations of C structures, how classes allow encapsulation by binding data and functions together, and general class declaration syntax. It also describes defining member functions inside and outside of classes, allocating memory for objects, and accessing class members using the dot operator.

Uploaded by

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

Classes and Objects: C++ 6 Sem, A' Div 2018-19 Ms. Mouna M. Naravani

The document discusses classes and objects in C++. It covers limitations of C structures, how classes allow encapsulation by binding data and functions together, and general class declaration syntax. It also describes defining member functions inside and outside of classes, allocating memory for objects, and accessing class members using the dot operator.

Uploaded by

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

Chapter 2

Classes and Objects

C++
6th Sem, ‘A’ Div
2018-19
Ms. Mouna M. Naravani
C Structures Revisited
Limitations of C Structure
1. struct complex Where as, this is perfectly ok…
{
c3.x = c1.x + c2.x
float x;
c3.y = c1.y + c2.y
float y;
};
struct complex c1, c2, c3;
c3 = c1 + c2 // This is illegal in C.
Limitations contd….
2. Structures do not permit data hiding. Structure members can be directly accessed by
structure variables by any functions anywhere in their scope.
That is, by default, the members of the structure are public.

3. Cannot write functions within a structure.


• It is a common practice to organize a
Structure of C++ Program program into three separate files.
• The class declarations are placed in a
header file and the definitions of
member functions go into another file.
• This approach enables the programmer
to separate the abstract specification of
the interface(class definition) from the
implementation details (member
function definition).
• Finally, the main program that uses the
class is places in a third file which
“includes” the previous two files as well
as any other file required.
Specifying a Class

• Class is a way to bind the data and its associated functions together(Encapsulation).

• The class specification has two parts:

• Class declaration - describes the type and scope of its members.

• Class function definitions - describes how the class functions are implemented.
The general form of class declaration:

Data members
and Member
functions are
collectively called
Class Members.
Terminated by ;
• Class members are usually grouped under two sections, private and public.
• private and public are keywords.
• These are known as visibility labels.
• NOTE: these keywords are followed by a colon(:).
• The class members that have been declared as public can be accessed from outside the
class.
• On the other hand, private members can be accessed only from within the class.
• The data hiding (using private declaration) is the key feature of OOP.
• By default, the members of a class are private.
Data hiding in classes
Defining member functions

Member functions can be defined in two places:

• Inside the class definition

• Outside the class definition


Defining member functions outside the class
definition

• Member function declaration is done inside a class.

• Member function definition is done outside the class.

• Member function incorporates a “membership identity label” .

• This label tells the compiler which class the function belongs to.
• The membership label class-name:: tells the compiler that the function
function-name belongs to the class class-name.
• The symbol :: is called the scope resolution operator.
Characteristics of Member functions:
• Several different classes can use the same function name. The ‘membership
label’ will resolve their scope.
• Member functions can access the private data of the class. A non-member
function cannot do so.
• A Member function can call another Member function directly, without
using dot operator.
When the memory is allocated?
Memory Allocation for Objects
➢ Memory space for objects is allocated when they are declared and not when the class is
specified.
➢ 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 data members is allocated separately for each object.
➢ Separate memory locations for the objects are essential, because the member variables
will hold different data values for different objects.
Creating Objects

• Once a class has been declared, we can create objects of that type by
using class name.

item x; // x is an object of class item, memory for x is created


• Objects can also be created when a class is defined by placing their
names immediately after the closing brace.
class item
{
----
----
} x, y, z;
Accessing Class Members
object-name . function-name(arguments);
Ex:
x . get_data();
x . put_data();

• Only get.data() has no meaning.

• x.number = 100; is illegal. Why?


• A variable declared as public can be accessed by the objects directly.
class demo
{
int x;
int y;
public:
int z;
-----
};

demo d;
d.x=10; //error
d.Z = 16; //OK

You might also like