classes and objects · classes and objects class declaration class members data constructors...
TRANSCRIPT
www.sahajsolns.com
Chapter 4Chapter 4
OOPS WITH C++ 1Sahaj Computer Solutions
www.sahajsolns.com
Session Objectives� Classes and Objects
� Class Declaration
� Class Members
Data Constructors� Data Constructors
� Destructors
� Member Functions
� Class Member Visibility
� Private, Public,Protected
� The scope of the class object constructor
OOPS WITH C++ Sahaj Computer Solutions 2
www.sahajsolns.com
Session objectives� Default Constructor
� Constructor with arguments
� Constructor with default arguments
Dynamic constructor� Dynamic constructor
� Copy Constructor
� Overloaded Constructor
� Objects as Function arguments
� Member functions defined outside the class.
� Objects as arguments
OOPS WITH C++ Sahaj Computer Solutions 3
www.sahajsolns.com
Session Objectives� Returning objects from functions
� Class array as class member data
� Array of objects
String as class member.� String as class member.
OOPS WITH C++ Sahaj Computer Solutions 4
www.sahajsolns.com
Accessing Class Members� To access the members of the class we make use of the
DOT(.) Operator.
� Syntax:
Object-name.function-name(actual arguments)Object-name.function-name(actual arguments)
� Example:
� x.getData(100,75.5);
OOPS WITH C++ Sahaj Computer Solutions 5
www.sahajsolns.com
Defining Member Functions� Member functions can be defined in two places.
� Outside the Class definition
� Inside the class definition
� Outside the Class Definition� Outside the Class Definition
� Member functions that are declared inside the class have to e defined separately outside the class.
� Their definition is much like normal functions.
� They should have a function header and a body.
OOPS WITH C++ Sahaj Computer Solutions 6
www.sahajsolns.com
Outside the Class Definition� An important difference between a member function and a
normal function is that a member function incorporates a membership ‘identity label’ in the header.
� This label tells the compiler which class the function � This label tells the compiler which class the function belongs to.
� The general form of a member-function definition is :
return-type class-name:: function-name(argument declaration)
{
Function-Body
}
OOPS WITH C++ Sahaj Computer Solutions 7
www.sahajsolns.com
Outside the Class Definition� The membership label class-name:: tells the compiler
that the function-name belongs to the class-name .
� That is the scope of the function is restricted to the class name specified in the header line.class name specified in the header line.
� The symbol :: is called the scope resolution operator.
� Example:void sum::getdata(int a,int b)
{
sum=a+b;
}
OOPS WITH C++ Sahaj Computer Solutions 8
www.sahajsolns.com
Defining Member Functions� The member functions have some special
characteristics that are often used in the program development:
� Several different classes can use the same function � 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 the dot operator.
OOPS WITH C++ Sahaj Computer Solutions 9
www.sahajsolns.com
Inside the class definition� Another method of
declaring a member function is to replace the function definition by the actual definition.
� Example
class sum{
int a,b;
public:the actual definition. public:
void getdata(int x,int y)
{
a=x;
b=y
}
};
OOPS WITH C++ Sahaj Computer Solutions 10
www.sahajsolns.com
Making Outside function Inline� A member function outside the class definition can still be
made inline by using the qualifier inline.� Example:
class item{::::::::::::::::::::::::::::::::::::public:
void getdata(int a,int b);};
inline void item::getdata(int a,int b){
::::::::::::::::::::::}
OOPS WITH C++ Sahaj Computer Solutions 11
www.sahajsolns.com
Nesting Member Functions� As discussed earlier a member function of a class can
be called by an object of that class using the dot operator.
� However there is an exception to this. � However there is an exception to this.
� A member function can be called by using its name inside another member function of that same class.
� This is known as nesting of member functions
� Program:CH4PG7.cpp
OOPS WITH C++ Sahaj Computer Solutions 12
www.sahajsolns.com
Arrays within a class� Arrays can be used as member variables in a class
� For example:
class arraydemo{
int a[20];int a[20];
public:
void getval(){
for(int i=0;i<n;i++)
cin>>a[i];
}
};
OOPS WITH C++ Sahaj Computer Solutions 13
Program: CH4PG8.cpp
www.sahajsolns.com
Memory Allocation for ObjectsMember variable 1
Member variable 2
Member function1
Class
OOPS WITH C++ Sahaj Computer Solutions 14
Member variable 1
Member variable 2
Member variable 1
Member variable 2
Member variable 1
Member variable 2
Member function1
Member function2
Object A Object B Object C
www.sahajsolns.com
Static Data Members � Whenever a object is created a new copy of data
variables is created for that object.
� This means that each objects works on its own copy of data variables.data variables.
� C++ uses another type of variable declaration called as static.
� A data member of class can be qualified as static
� The static member variable has certain special characteristics.
OOPS WITH C++ Sahaj Computer Solutions 15
www.sahajsolns.com
Static Data Members� 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 � 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.
OOPS WITH C++ Sahaj Computer Solutions 16
www.sahajsolns.com
Static Data Members� PROGRAM:CH4PG9.CPP
Note that the type and scope of each static member
int item :: count ;
� 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 class itself rather than objects, they are known as class variables.
OOPS WITH C++ Sahaj Computer Solutions 17
www.sahajsolns.com
Static Data Members
number number number
Object A Object B Object C
OOPS WITH C++ Sahaj Computer Solutions 18
count
Common to all three objects
www.sahajsolns.com
Static Member Functions� Like static member variables, C++ has static member
functions.
� A member function that is declared as static has following properties: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 object)
� Syntax: class-name::function-name;
� Program: Ch4Pg10.cpp
OOPS WITH C++ Sahaj Computer Solutions 19
www.sahajsolns.com
Array of Objects� Arrays can be of any data type including class and
struct.
� Such variables are called array of objects.
� Syntax;� Syntax;
class-name object-name[size];
� Example:
employee manager[20];
� Program :CH4PG11.cpp
OOPS WITH C++ Sahaj Computer Solutions 20
www.sahajsolns.com
Objects as function arguments� Like any other data type, an object may be used as a
function argument.
� This can be done in two ways:
� A copy of the entire object is passed to the function.� A copy of the entire object is passed to the function.
� Only address of the object is transferred to the function.
� Program: CH4PG12.cpp
OOPS WITH C++ Sahaj Computer Solutions 21
www.sahajsolns.com
Friend Functions� It is possible to grant a non-member function access to
the private members of a class by using a friend.
� A friend function has access to all private and protected members of the class for which it is a friend. protected members of the class for which it is a friend.
� To declare a friend function, include its prototype within the class, preceding it with the keyword friend.
� Syntax:
friend type fn-name(arg-list){
//function body
} Program:CH4PG13.cpp
OOPS WITH C++ Sahaj Computer Solutions 22
www.sahajsolns.com
Friend Function� In this example, the sum() function is not a member of
myclass.
� However, it still has full access to its private members.
� Also, notice that sum() is called without the use of the � Also, notice that sum() is called without the use of the dot operator.
� Because it is not a member function, it does not need to be (indeed,it may not be) qualified with an object's name.
OOPS WITH C++ Sahaj Computer Solutions 23
www.sahajsolns.com
Friend Function� 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 normal function without the help of any object.
� It can be declared in public or private part of the class without affecting its meaning.
� Usually its has objects as its arguments.
OOPS WITH C++ Sahaj Computer Solutions 24
www.sahajsolns.com
A function friendly to two classes� Program:CH4PG14.cpp
� The function max() has arguments from both XYZ and ABC.
� When the function max() is declared as a friend � When the function max() is declared as a friend function in XYZ for the first time, the compiler will not acknowledge the presence of ABC unless its name is declared in the beginning as
class ABC;
� This is known as forward declaration
OOPS WITH C++ Sahaj Computer Solutions 25
www.sahajsolns.com
Friend Classes� It is possible for one class to be a friend of another
class.
� When this is the case, the friend class and all of its member functions have access to the private members member functions have access to the private members defined within the other class.
� For example,
class Y {
friend class X;
};
Program : CH4PG15.cpps
OOPS WITH C++ Sahaj Computer Solutions 26
www.sahajsolns.com
Defining Inline Functions Within a
Class� It is possible to define short functions completely
within a class declaration.
� When a function is defined inside a class declaration, it is automatically made into an inline function (if it is automatically made into an inline function (if possible).
� It is not necessary (but not an error) to precede its declaration with the inline keyword.
� Program : CH4PG16.cpp
OOPS WITH C++ Sahaj Computer Solutions 27
www.sahajsolns.com
Returning Objects� A Function can not only receive objects as arguments
but also can return them .
� The following program demonstrates this concept.
� Program: CH4PG17.cpp� Program: CH4PG17.cpp
OOPS WITH C++ Sahaj Computer Solutions 28
www.sahajsolns.com
Structures and Classes Are Related� A class is syntactically similar to a struct. But the
relationship between a class and a struct is closer thanyou may at first think.
� In C++, the role of the structure was expanded,� In C++, the role of the structure was expanded,making it an alternative way to specify a class.
� In fact, the only difference between a class and a structis that by default all members are public in a struct andprivate in a class.
OOPS WITH C++ Sahaj Computer Solutions 29
www.sahajsolns.com
Unions and Classes Are Related� Like a structure, a union may also be used to define a
class.
� In C++, unions may contain both member functionsand variables.and variables.
� They may also include constructor and destructorfunctions.
� A union in C++ retains all of its C-like features, themost important being that all data elements share thesame location in memory.
OOPS WITH C++ Sahaj Computer Solutions 30
www.sahajsolns.com
Constructors� A constructor is a special member function whose task is
to initilize the object of its class.
� The constructor is invoked whenever the object of the class is created.
Syntax: Example:� Syntax:
class class-name{
public:
class-name() //constructor
{
}
};
OOPS WITH C++ Sahaj Computer Solutions 31
Example:class integer{
int x,y;public:integer(){
x=0;y=0;
}};
www.sahajsolns.com
Constructors� Characteristics of constructors;
� They should be declared in public section.
� They have no return type not even void and therefore they do not return values.they do not return values.
� They cannot be inherited , though derived class constructors can call base class constructor.
� Constructors cannot be virtual.
� Program: CH4PG18.cpp
OOPS WITH C++ Sahaj Computer Solutions 32
www.sahajsolns.com
Types of constructors� There are three types of constructors in C++:
� Default Constructors
� Parameterized Constructors
� Copy constructors� Copy constructors
� Default Constructors:
� A default constructor is a constructor which does not accept any arguments.
OOPS WITH C++ Sahaj Computer Solutions 33
www.sahajsolns.com
Parameterized Constructors� A constructor which accepts arguments is called as
parameterized constructor.
� Syntax:
class-name :: class-name(argument-list)class-name :: class-name(argument-list)
{
//body of the constructor
}
� Example:
OOPS WITH C++ Sahaj Computer Solutions 34
class integer{int x,y;
public:integer(int a, int b){
x=a;y=b
}};
www.sahajsolns.com
Parameterized Constructors� An parameterized constructor can be called using two
ways
� By calling the constructor explicitly
� By calling the constructor implicitly� By calling the constructor implicitly
� Examples:
integer i=integer(10,20); //explicit call
integer i(10,20); //implicit call
Program:CH4PG19.CPP
OOPS WITH C++ Sahaj Computer Solutions 35
www.sahajsolns.com
Constructor Overloading� A class can have one or more than one constructors
defined in a class with different number of arguments or different type of arguments.
� This is called constructor overloading.� This is called constructor overloading.
� The compiler recognizes the constructor depending upon the number of arguments or type of arguments.
� Program: CH4PG
OOPS WITH C++ Sahaj Computer Solutions 36
www.sahajsolns.com
Copy Constructor� A Constructor which accepts an copy of its own class as
an argument is called an copy constructor.
� Syntax:
class-name:: class-name(class-name &obj)class-name:: class-name(class-name &obj)
{
//constructor body
}
� A copy constructor is used to declare and initialize an object from another object.
OOPS WITH C++ Sahaj Computer Solutions 37
www.sahajsolns.com
Dynamic Constructor� The constructor can also be used to allocate memory
while creating objects.
� This ensures the system to allocate the right amount of memory for each object when the objects are not of memory for each object when the objects are not of the same size, thus resulting in the saving of memory.
� Allocation of memory to objects at the time of their construction is known as dynamic constructor.
OOPS WITH C++ Sahaj Computer Solutions 38
www.sahajsolns.com
Constructors with Default
Arguments� It is possible to define constructors with default
arguments.
� For example:
Complex( float real, float img=0);Complex( float real, float img=0);
� The argument img=0. Then the statement
� Complex(5.0);
� Assigns 5.0 to real and 0 to img (by default) variables.
� Complex(2.0,3.0);
� Will assign 2.0 to real part and 3.0 to img variables.
OOPS WITH C++ Sahaj Computer Solutions 39
www.sahajsolns.com
Sahaj Computer Solutions Near Gomatesh School, Hindwadi Belgaum-11, Phone no: 4200864,2481703
Get Certified in
•Microsoft Office 2010•Tally 9.2•.NET 2008•J2EE•CORE JAVA
� Around 1800 students developed software projects and scored the top scores in exams.
� One stop shop for •CORE JAVA•C PROGRAMMING•OOPS WITH C++•JOOMLA•WORDPRESS•PHP AND MYSQL•SQLSERVER•UNIX AND LINUX•ANDRIOD APPS
� One stop shop for � Software Projects
� Website Development
� Technology Training
� I.T Research Visit: www.sahajsolns.com
OOPS with C++ Sahaj Computer Solutions 40