“CLASS
AND
OBJECT”
C++ STRUCTURE
• C++ structure supports all the features of C structure as well as it has expanded capabilities.
• Syntax:
struct tag_name
{
Data members;
Member function
}variable;
• Alternative way to create variable is, write tag_name variable; inside main()
Example 1:
C++ STRUCTURE FEATURES struct trial
{
1. The C++ structure names are stand alone and int a;
can be used like any other type names means };
built-in data types var1 =var2 + var3; // allowed using operator overloading concept.
2. Data members by default they are public
member.
3. Structure can initialize the data members;
4. Can have member function as a part of
structure.
5. Can be declare variable as built-in data type.
6. Inheritance, a mechanism by which one type
can inherit characteristics from other types, is
also supported by C++.
5
CLASS
• C++ incorporates all these extension in another user-defined type known as Class.
• A very little difference C++ structure and Class.
• By default all the members of class in private while C++ structure is public.
• Definition:
A class is a way to bind the data and its associated functions together.
• It allows both data and functions to be hidden.
• When we defining a class, we are creating a new abstract data type that can be treated like any other built-in data
type.
• Generally a class specification has two parts:-
• a) Class declaration
• b) Class function definition
6
CLASS
• The classdeclaration describes the type and scope of its members.
• The class function definition describes how the class functions are implemented.
• Syntax:-
class class-name
{
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
7
COMPARISON TABLE OF C STRUCTURE, C++
STRUCTURE AND CLASS
Characteristics C Structure C++ Structure Class
Member variable Yes Yes Yes
Member function No Yes Yes
Support Constructor No Yes Yes
Support Destructor No Yes Yes
Access control No Yes Yes
Default Access public public private
Inheritance No Yes Yes
Empty structure 0 1 byte 1 byte
Direct initialization No Yes Yes
Struct keyword for Yes No -
declaring variable
Static variable No Yes Yes 8
SIZE OF STRUCTURE
C STRUCTURE C++ STRUCTURE
#include<stdio.h> #include<iostream>
struct t using namespace std;
{ }; struct t
int main() { };
{ int main()
struct t var; {
printf("%d",sizeof(var)); cout<<sizeof(t);
return 0; return 0;
} }
Output: ?? Output: ??
0 byte 1 byte
7
WHY IS THE SIZE OF AN EMPTY CLASS /
STRUCT 1 BYTE IN C++?
• The C++ standard does not allow objects (and classes thereof) of size 00, since that would make it possible
for two distinct objects to have the same memory address. That's why even empty classes must have a size
of (at least) 1.
• The C++ standard also states that no object shall have the same memory address as another object. There are
several good reasons for this.
1. To guarantee that new will always return a pointer to a distinct memory address.
2. To avoid some divisions by zero. For instance, pointer arithmetic's (many of which done automatically by the
compiler) involve dividing by sizeof(T).
• Note: if class has inheritance then it avoid to allocate 1 byte as its derived class has members.
CLASS IN DETAIL
• Syntax:- Keyword
class class-name Abstract Data type
{
private :
variable declarations;
Visibility Labels / access specifier
function declaration ;
Class Members
public :
variable declarations;
function declaration;
}; Terminated by Parentheses
• The memberss are declared as private can be accessed only from with in the class.
• The public members can be accessed from outside the class.
• The data hiding is the key feature of oops.
• The use of keywords private is optional by default, the members of a class are private.
CLASS DATA / DATA MEMBERS
• The data items within a class are called data members (or sometimes member data).
◦ OR
◦ The variables declared inside the class are known as data members
• There can be any number of data members in a class, just as there can be any number of data
items in a structure.
MEMBER FUNCTIONS
Member functions are functions that are included within a
class.
Synonym is Methods
Only the public member functions can have access to
the private data members and private functions
NOTE: The binding of data and functions together into a
single class type variable is referred to as encapsulation.
EXAMPLE
1)
class item
{
int number;
float cost;
public:
void getdata (int a ,float b);
void putdata (void);
};
PRIVATE AND PUBLIC
The body of the class contains two unfamiliar keywords: private and public. What is their purpose?
A key feature of object-oriented programming is data hiding.
Data is concealed within a class so that it cannot be accessed mistakenly by functions outside the
class. The primary mechanism for hiding data is to put it in a class and make it private.
Private data or functions can only be accessed from within the class. Public data or functions, on the
other hand, are accessible from outside the class.
HIDDEN FROM WHOM?
Don’t confuse data hiding with the security techniques used to protect computer databases.
To provide a security measure you might, for example, require a user to supply a password before granting access to a
database. The password is meant to keep unauthorized or malevolent users from altering or often even reading the
data.
Data hiding, on the other hand, means hiding data from parts of the program that don’t need to access it. More
specifically, one class’s data is hidden from other classes. Data hiding is designed to protect well-intentioned
programmers from honest mistakes.
14
WHY GENERALLY FUNCTIONS ARE PUBLIC,
DATA IS PRIVATE?
Usually the data within a class is private and the functions are public.
The data is hidden so it will be safe from accidental manipulation,
while the functions that operate on the data are
public so they can be accessed from outside the class.
However, there is no rule that says data must be private and
functions public; in some circumstances you may find
you’ll need to use private functions and public data.