Unit - II
Unit - II
Introduction to class
• The most important feature of C++ is the
“CLASS”
•A class is an extension of the idea of structure
used in C.
•It is a new way of creating & Implementing a
user-defined data type.
•Ex:
struct complex
{
float x;
float y;
}; struct complex C1,C2,C3;
•The complex numbers C1,C2,C3 can easily be assigned values
using the dot operator, but we cannot add two complex numbers
or subtract one from the other.
C3 = C1+C2; illegal.
•Another important limitation is 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++ has many characteristics to extend the C language into the
OOPS concepts.
•So all these incorporates in C++, named as class.
INTRODUCTION
• C++ class mechanism allows users to define
their own data types that can be used as
conveniently as build in types.
• // body of a class
• }
Cont…………….
• The class definition has two parts:
• Class head : composed of keyword class
followed by the class name
• Class body: enclosed by a pair of curly
braces.
General form of class declaration
• Class className
• {
• Access_specifier_1:
• Members;
• Access_specifier_2:
• Members;
• ……
• }
• objectName;
Cont…………
• The objectName is optional.
• If present, it declares objects of the class.
Example of student class
• Class student
• {
• Int rollNo; (data members)
• Char name [25];
• Char addr [35];
• Public:
• Void getdata(); (member functions)
• Void display();
• }
• ;
Cont………
• The class data members and member
functions are declared within the body of the
class along with their access levels.
• The class body defined the class member list
and their scope.
• If two classes have members with the same
name, the program will not show any error
since the members refer to different objects.
Access specifier
• Public
• Private
• Protected
Public
• Public members may be accessed by member
functions of same class and functions
outside the scope of the class (anywhere
inside the program)
Private
• Private members may only be accessed by
member functions and friend function of the
class.
• A class that enforces information hiding
declares its data members as private.
Protected
• The protected members may be accessed only
by the member functions of its class or by
member functions of its derived class.
Cont………..
• The data hiding is achieved by declaring data
members in the private section of the class.
• Since the private members are not accessible
from outside the class, they will be accessed
by the publicly declared member functions of
the same class.
Defining a class
class box
{
Public: Int a,b,c; Data Members
void get()
{
cin>>a>>b>>c;
}
void put()
{ Member Functions
cout<<a<<b<<c;
}
};
IT 3rd Sem
Object-Oriented Programming:
Class:
class
C++ <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function
declarations
access-specifier:
variable declarations
function
declarations
Example
• Class rectangle
• {
• Int length;
• Int width;
• Public:
• Void setvalues (int,int);
• Int area();
• };
Cont………..
• It declares a class rectangle. The class contains
four members:
• Two data members length and width of type
integer in the private section (because private
section is default permission)
• Two member functions:
• Setvalues (int, int) and area() in the public
section.
IT 3rd Sem
C+
Object-Oriented Programming:
Object:
class
+ <class-name>
{
access-specifier:
variable declarations
function declarations
access-specifier:
variable declarations
function
declarations
} ob1;
//object
void creation
main()
{
<class-name> ob2; //object
creation
Objects
Object-Oriented Programming
42
Using C++, Third Edition
Implementing Class Functions
(continued)
Object-Oriented Programming
43
Using C++, Third Edition
Memory allocation for Objects
•Memory space for objects is allocated when they
are declared and not when the class is specified. This
statement is partly true.
2. Parameterized Constructor
3. Copy Constructor
4. Dynamic Constructor
Default constructor
1. A constructor without any argument is known as default
constructor.
2. If no constructor defined in the class, then compiler
implicitly provides an empty body constructor which is
called as default constructor.
Ex: class circle
{
float radius;
Public:
circle( )
{
radius = 0;
}
};
Parameterized Constructor
1. Sometimes it is necessary to initialize the various
data elements with different values when they
are created.
2. This is achieved by passing arguments to the
constructor function when the objects are
created.
3. The constructors that can take arguments are
called Parameterized Constructors
Two ways of calling a constructor
1. Implicit call(shorthand method)
circle ob(7.6);
2. Explicit call
circle ob;
ob = circle(7.6);
Copy Constructor
1. A copy constructor is used to declare and initialize an
object from another object.
integer(integer & i);
integer I2(I1) or integer i2=i1;
2. The process of initializing through a copy constructor is
known as copy initialization.
3. The statement i2=i1 will not invoke the copy constructor
4. If i1 and i2 are objects, this statement is legal and assigns
the values of i1 to i2, member-by-member.
5. A reference variable has been used as an argument to the
copy constructor.
6. We cannot pass the argument by value to a copy
constructor.
Dynamic constructors
1. The constructors can also be used to allocate
memory while creating objects.
2. This will enable the system to allocate the right
amount of memory for each object when the
objects are not of the same size.
3. Allocation of memory to objects at the time of
their construction is known as dynamic
construction of objects.
4. The memory is allocated with the help of the
new operator.
circle ob(100)
Dynamic initialization of objects
1. Providing initial values to objects at run time.
2. Advantages –
We can provide various initialization formats,
using overloaded constructors.
This provides the flexibility of using different
format of data at run time depending upon the
situation.
Constructors with default arguments
1. It is possible to define constructors with default
arguments.
complex(float real, float imag=0)
2. The default value of the argument imag is zero. Then the
statement
complex c(5.0); assigns the value 5.0 to the real variable
& 0.0 to imag(by default)
ex : complex c(2.0,3.0)
A : : A( ) default constructor
A : : A (int = 0) Default argument constructor
3. The default argument constructor can be called with
either one argument or no arguments.
4. When called with no arguments, it becomes a default
constructor
Multiple constructors in a class
1. C++ permits to use more than one constructor in a class.
2. Add( ); no arguments
3. Add(int, int) two arguments
class add
{
int m,n;
Public:
add ( ){ m=0; n=0;} first constructor
add(int a, int b) second constructor
{ m=a; n=b; }
add(add &i) third constructor
{ m=i.m; n=i.n;}
};
Destructors
1. As the name implies, it is used to destroy the objects that
have been created by a constructor.
2. Like a constructor, the destructor is a member function
whose name is the same as class name but is proceded by a
tilde.(~)
~integer()
{
}
3. It never takes any argument nor does it return any value.
4. It will be invoked implicitly by the compiler upon exit from the
program to clean up storage that is no longer accessible.
5. It is good practice to declare destructors in a program since it
releases memory space for future use.
Objects as Function Arguments
An object may be used as an function argument.
This can be done by two ways,
1. A copy of the entire object is passed to the function
This is called pass by value. Since a copy of the object is
passed to the function, any changes made to the object
inside the function do not affect the object used to call
the function.
2. Only the address of the object is transferred to the
function
This is called pass-by-reference. When an address of the
object is passed, the called function works directly on
the actual object used in the call. This means that any
changes made to the object inside the function will
reflect in the actual object.
Returning objects
A function can not only receive objects as
arguments but also can return them.
This is called returning objects
The this Pointer and Constant
Member Functions
• this pointer:
- Implicit parameter passed to a
member function (by the compiler)
- points to the object calling the function
• const member function:
- does not modify its calling object
C+
Object-Oriented Programming:
+
Types of Member Functions:
Inline Functions
Nested Functions
Friend Functions
Static Functions
Virtual Functions
FRIEND FUNCTION
• The protected and private members cannot be
accessed from outside the same class at
which they are declared.
• Its possible to grant a non member function
access to the private members of a class,
by using a keyword friend.
Cont………
• A friend function has access to all private and
protected members of the class for which it is
friend.
Syntax of friend function
• Class rectangle
• {
• ……
• …..
• Public:
• ……
• ……
• Friend rectangle duplicate (rectangle)
• }
Characteristics of friend function
1. It is not member function of the class.
2. It is like normal external functions.
3. It is not in the scope of the class to which it
has been declared.
4. It can be declared either public or private
section of the class.
5. Usually it passes objects as arguments.
Friends of Classes
* vector<>
* lists<>
* stack<>
* queue<>
* deque<>
Cont…………
• Associative Container Classes
* map<>
* set<>
* multimap<>
* multiset<>
Storage classes
• Used to specify the lifetime and scope of the
variables.
• How storage is allocated for variables and how
variable is treated by complier depending
upon these storage classes.
5 types
1. Local variable
2. Global variable
3. Register variable
4. Extern variable
5. Static variable
Namespace
• Container for identifiers.
• Puts the names of its member in a distinct
space so that they don’t conflict with the
names in other namespaces.
Synta
•
x
Its creation is similar to class creation
• Namespace Myspace
• {
• Declarations
• }
• Int main() {}
Will create namespace named Myspace, in
which we put member declarations.
Class member function
• A member function of a class is a function that
has its definition or its prototype within the
class definition like any other variable.
Class access modifier
• A class member can be defined as public,
private or protected. By default
members would be assumed as private.