class - UPES - Tech Community

Lecture 3 (UNIT -1)
SUNIL KUMAR
CIT-UPES
| Jan 2016|
© 2012 UPES
Objectives
Upon completion of today lecture, You will be : Able to understand the basic concept of object oriented programming.
 Able to understand Class , Objects, Class Members, Access Control,
Parameter Passing.
 Able to use function inside and outside the class.
 Able to understand the functions with Parameter Passing
Jan
Jan2016
2016
© 2012 UPES
Structures ( revision)
Keyword
 A
way to combine different typesName
of dataofinto a single unit with
Structure
continuous memory location
 Tool for handling group of logically related data items
struct student
{
char name[20];
Three fields with diff. data
int roll_num;
three fields
with diff.members
data or
types-structure
typeselements
float total_marks; structure members or
elements
};
Jan
Jan2016
2016
© 2012 UPES
Structures Revised
Keyword
Name
of to create variable of type
 Structure
name (student) can be
used
Structure
student. Example:
struct student A;
// C declaration
 Dot opertaor is used for accessing member variables as:
strcpy(A.name, “John”);
A.roll_num =25;
A.total_marks =550;
Declaration of
a structure
type variable
Assigning value to
member variables
final_result = A.total_marks + 20;
 Structures can have array, pointers or structures as members.
Jan
Jan2016
2016
© 2012 UPES
Structures Revised
 Limitations of C structures:
►
Struct data type can not be treated as built-in types, as shown:
struct sum
{
float x;
float y;
};
struct sum a, b, c;
Values can easily be assigned to a, b, c using dot operator but we can not add or
subtract one from other, as shown:
a= b+c; // illegal in C
► Secondly, no data hiding in structures i.e. structure members can be directly
accessed by other structure variables by any function anywhere in their scope.
Means structure members are public members.
Jan
Jan2016
2016
© 2012 UPES
Extensions to Structure
 C++ supports all the features of structures in C but expanded its
capabilities to suits OOP philosophy
 User-defined data types to be treated as close as possible to the
built-in data types
 Feature of DATA HIDING (one of the main principles of OOP)
 In C++, both variables and functions be used as members
 Some members can be declared as private so that can not be
accessed by others directly.
 Keyword struct can be omitted in the declaration of structure variable,
example:
student A; // legal in C++ but illegal in C
Jan
Jan2016
2016
© 2012 UPES
Extensions to Structure
 C++ includes all these extensions in another userdefined type called class.
 Small syntactical difference b/w structures and classes
in C++, hence one can use both interchangeably with
minor modifications.
 Most of the programmers use structures for holding data
and classes for both data and functions.
 The only difference between structure and class in C++
is that, by default, members of class is private while
public in case of structure.
Jan
Jan2016
2016
© 2012 UPES
Class
 A way to bind data and functions together and allows data to be
hidden, if required, from external use
 While defining class, we are creating a new abstract data type that
can be treated as any other built-in data type.
 Two parts of class specification:
►Class
declaration- describes the type and
scope of its members
►Class
function definitions- describes how
the class functions are implemented.
Jan
Jan2016
2016
© 2012 UPES
General
declaration of Class
Keyword
Name of
a Class
class class_name
{
private:
variable declarations;
Access
Specifier
function declarations;
public:
variable declarations;
function declarations;
};
Jan
Jan2016
2016
Data
hiding
Class members
© 2012 UPES
Class
 Variable declared inside class are- data members and function
are- member functions.
 Only member functions can have access to the private data
members and private functions
 Binding of data and functions together into a single class-type
variable is referred to as encapsulation.
Jan
Jan2016
2016
© 2012 UPES
Class
 Simple example:
class item
{
int number;
//variables declaration
float cost;
//private by default
public:
void getdata(int a, float b);
// function declaration
void putdata(void);
};
Jan
Jan2016
2016
© 2012 UPES
Object Creation
 Remember- class declaration does not define any object of class but only
specifies what they contain
 Once class is declared, we can create variable of that type by using the class
name, say
►
item z;
►
Here the class variables are known as objects, so z is object of type
item
►
One can declare more than one object in one statement, say- item a,
b, c;
►
Also possible way, like in structures, for creating objects is:
// memory for z is created
Class item
{
…….
} a, b, c;
Jan
Jan2016
2016
© 2012 UPES
Accessing Class Members
 The main() cannot contain statements that access data members
directly. They can only be accessed through member functions.
 Syntax for accessing class members-
object-name.function-name(actual-arguments);
Example: x.getdata(500, 42.5); // is valid for
object z
Similarly- x.putdata();
values
Jan
Jan2016
2016
//would display
© 2012 UPES
Accessing Class Members
 Invalid statements-
►
►
►
getdata(100, 42.5);
x.number= 300; //such statements have no meaning
Variable declared as public can be accessed by objects directly, as
shown:
class xyz
{
int x;
int y;
public:
int z;
};
………
xyz p;
p.x =10;
p.z =20
…........
Jan
Jan2016
2016
//error, x is private
// accepted, z is public, should be avoided (defeats idea of data hiding)
© 2012 UPES
Defining Member function
 Two places:
►Outside
►Inside
the class definition
the class definition
 Irrespective of the place of function definition, the function should
perform the same task.
Jan
Jan2016
2016
© 2012 UPES
Outside the Class Definition
 Difference b/w member function and normal function is that a member
function incorporates a membership ‘identical label’ in the header.
 This ‘label’ tells the compiler which class the function belongs to.
 Member function definition:
return-type class-name :: function-name
(argument declaration)
{
Function body
}
Jan
Jan2016
2016
© 2012 UPES
Outside the Class Definition
 :: scope resolution operator, tells the compiler that the function functionname belongs to the class class-name.
 This means, scope of the function is restricted to the class-name specified in
the header line.
Void item :: getdata( int a, float b)
{
Number = a;
Cost = b; }
Void item :: putdata(void)
{
Cout<< “number :”<< number << “\n”;
Cout<< “cost :”<< cost << “\n”;
}
Jan
Jan2016
2016
© 2012 UPES
Outside the Class Definition
 Characteristics of member functions:
►Several
different classes can use the same
function name.
►Member
functions can access the private
data of the class.
►A
member function can call another member
function directly, without using the dot
operator.
Jan
Jan2016
2016
© 2012 UPES
Inside the Class Definition
 Example:
class item
{
int number;
Float cost;
Public:
Void getdata(int a, float b);
Void putdata(void)
//treated as inline function
{
Cout << number << “\n”;
Cout << cost << “\n”;
}
};
Jan
Jan2016
2016
© 2012 UPES
Inside the Class Definition
 Function defined inside class is treated as inline function.
 Hence all restrictions and limitations that apply to an inline function
are also applicable here.
 Only small functions are defined inside the class definition.
Jan
Jan2016
2016
© 2012 UPES
C++ with class
# include <iostream.h>
Class item
{
Int number;
Float cost;
Public:
Void getdata(int a, float b);
Void putdata(void)
{
Cout<<“number :” <<number<< “\n”;
Cout<<“cost :” <<cost<< “\n”;
}
};
Void item :: getdata(int a, float b)
{
Number=a;
Cost=b;
}
Jan
Jan2016
2016
© 2012 UPES
C++ with class
int main ()
{
item x;
cout<< “\nobject x” << “\n”;
x.getdata (200, 140.55);
x.putdata();
item y;
cout<< “\nobject y” << “\n”;
y.getdata (700, 159.32);
y.putdata();
return 0;
}
Jan
Jan2016
2016
© 2012 UPES
 When a member function can be
called by using its name inside
another member function of the
same class, it is known as
nesting of member function.
# include<iostream.h>
class set
{
int m, n;
public:
void input (void);
void display (void);
int largest(void);
};
Jan
Jan2016
2016
int set:: largest (void)
{
if (m >= n)
return (m);
else
return (n);
}
void set : : input (void)
{
cout << “input values
of m & n:”;
© 2012 UPES
Nesting of member functions
int set:: largest (void)
void set :: display(void)
{
{
if (m >= n)
cout << “largest value = “ <<largest();
//calling member function
return (m);
}
else
return (n);
main()
{
}
void set : : input (void)
set a
a.input();
{
cout << “input values of m &
n:”;
a.display();
}
cin >> m >> n;
}
Jan
Jan2016
2016
© 2012 UPES