0% found this document useful (0 votes)
62 views50 pages

EEN 103 Programming in C++

This document provides an overview of object-oriented programming concepts in C++ including data hiding, abstract data types, classes, access control, class implementation using default constructors, copy constructors, destructors, and operator overloading. It discusses the use of pointers in linked data structures and object-oriented design principles like inheritance and composition. Key concepts covered are dynamic binding, virtual functions, and polymorphism. It also describes special member functions in C++ like constructors and destructors that affect how objects are created and destroyed.

Uploaded by

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

EEN 103 Programming in C++

This document provides an overview of object-oriented programming concepts in C++ including data hiding, abstract data types, classes, access control, class implementation using default constructors, copy constructors, destructors, and operator overloading. It discusses the use of pointers in linked data structures and object-oriented design principles like inheritance and composition. Key concepts covered are dynamic binding, virtual functions, and polymorphism. It also describes special member functions in C++ like constructors and destructors that affect how objects are created and destroyed.

Uploaded by

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

EEN 103

PROGRAMMING IN C++

Object Oriented Programming Concepts: Data hiding, abstract data types,


classes, access control; Class implementation-default constructor, constructors,
copy constructor, destructor, operator overloading, friend functions; Introduction
to data structures, use of pointers in linked structures; Object oriented design (an
alternative to functional decomposition) inheritance and composition; Dynamic
binding and virtual functions; Polymorphism; Dynamic data in classes.
• C++ defines several kinds of functions that can
be declared only as class members
— these are called "special member functions."

• These functions affect the way objects of a


given class are created, destroyed, copied,
and converted into objects of other types.

• Another important property of many of these


functions is that they can be called implicitly
(by the compiler).
Will Compiler
Is Function
Can Function Is Function a Generate
Function Inherited Can Function
Return a Member or Function if
Type from Base Be Virtual?
Value? Friend? User Does
Class?
Not?
Constructor No No No Member Yes
Copy
No No No Member Yes
Constructor
Destructor No Yes No Member Yes
Conversion Yes Yes No Member No
Assignment
No Yes Yes Member Yes
(operator=)
Static
new Yes No void* No
member
Static
delete Yes No void No
member
Other
member Yes Yes Yes Member No
functions
Friend
No No Yes Friend No
functions
Constructors
and
Destructors
• There are two Special members functions
1. Constructors
2. Destructors

• Constructors are called every time we


create an object.

• Destructors are called every time we


destroy an object.
Constructors

• Default Constructors
• Parameterized Constructors
• Copy Constructors
• A constructor is a special member function whose
task is to initialize the objects of its class. This is
known as automatic initialization of objects.

• It is special because its name is the same as the


class name.

• The constructor is invoked whenever an object of


its associated class is created.

• It is called constructor because it constructs the


values of data members of the class.
Why constructors
• First a variable declared.
Example:
int x; //variable x of type integer
float y; //variable y of type float
• Variable x or y would have garbage value before
initialization.

• We needed to initialize the variable to 0 or to some


other useful value before using it.
– Example:
int x =0; //variable x of type integer initialized to 0
float y= 22.5 ; //variable y of type float initialized to 22.5
Why constructors
• The same is true of objects. i.e. declaration and
initialization of objects
• The difference is that with an object, we can't
just assign it a value. We can't say:
Student s1 = 0; because that doesn't make sense.
(here s1 is an object of class Student)
A Student is not a number, so we can't just set it to
0.
The way object initialization happens in C++ is that a
special function, the constructor, is called when you
instantiate an object.
Without constructor
small_rect.set_values (4,6);
big_rect.set_values (7,5);

With constructor
CRectangle small_rect (4,6);
CRectangle big_rect(7,5);
• Constructors can not be called explicitly as if
they were regular member functions.

• They are only executed when a new object of


that class is created.

• Neither the constructor prototype declaration


(within the class) nor the latter constructor
definition include a return value; not even void.
Parameterized Constructors
(Syntax Constructor Definition)
Class_name::Class_name(parameter1, ..., parametern)
{
statements
}
Example:
Point::Point(double xval, double yval)
{
x = xval; y = yval;
}
Overloading Constructors
• Like any other function, a constructor can also be
overloaded with more than one function that
have the same name but different types or
number of parameters.

• For overloaded functions the compiler will call the


one whose parameters match the arguments
used in the function call.
• In the case of constructors, which are
automatically called when an object is create

• The one executed is the one that matches the


arguments passed on the object declaration.
Default Constructor
• A constructor that accepts no parameters is
called default constructor.
• The default constructor for class CRetangle is
CRectangle::CRectangle ()
{ width = 0; height = 0; }
• If no such constructor is defined, then compiler
supplies default constructor. Therefore a
statement such as
• Crectangle small_rect; invokes the default
constructor of the compiler to create the object
small_rect
Using Default Constructor with
parameters
• If a new object is declared and want to use its
default constructor (the one without
parameters), we do not include parentheses ():

• CRectangle rectb; // right


• CRectangle rectb(); // wrong!
Characteristics of Constructors
• They should be declared mostly in public
section
• Invoked automatically when objects are
created
• Do not have return type, not even void. They
can not return values.
Summary
• An constructor is called every time an object is
instantiated
• Uses of constructor
– Initialize data members when required
(data members can not be initialized in the class declaration because
each object has its own variables and must be initialized with the
values required by the object being created)
– To validate data and thus make the program more robust
– To allocate memory for an object
• Name of the function is same as the class
name. No return type, not even void
• A class must have at least one constructor,
either defined by the program or by the
compiler
• Default constructor is called whenever an
object is created without passing any
arguments
• If we define any type of constructor, we must
also define a default constructor, if it is
needed
If we do not specify any
constructor
• Three special member functions in total that
are implicitly declared if we do not declare our
own constructor(explicitly)
• These are
– the default destructor
– the copy constructor,
– the copy assignment operator
• Copy constructor is a constructor function
with the same name as the class
• used to make deep copy of objects.
• There are 3 important places where a
copy constructor is called.
1. When an object is created from another
object of the same type
2. When an object is passed by value as a
parameter to a function
3. When an object is returned from a
function (unless return value optimization applies- the
return value optimization (RVO) is a compiler
optimization that involves eliminating the temporary
object created to hold a function's return value. This will
depend upon the compiler)
• If a copy constructor is not defined in a class,
the compiler itself defines one. This will
ensure a shallow copy.
• If the class does not have pointer variables
with dynamically allocated memory, then one
need not worry about defining a copy
constructor. It can be left to the compiler's
discretion.
• But if the class has pointer variables and has
some dynamic memory allocations, then it is a
must to have a copy constructor.
• For ex:
      class A   //Without copy constructor
      {
           private:
           int x;
           public:
           A() {x = 10;}
           ~A() {}
};
class B    //With copy constructor
      {
           private:
           char *name;
           public:
           B()
           {
           name = new char[20];
           }
           ~B()
           {
           delete [] name;
           }
     //Copy constructor
           B(const B &b)
           {
           name = new char[20];
           strcpy(name, b.name);
           }
      };
• Let us imagine if you don't have a copy constructor for the
class B. At the first place, if an object is created from some
existing object, we cannot be sure that the memory is
allocated. Also, if the memory is deleted in destructor, the
delete operator might be called twice for the same memory
location.
• This is a major risk. One happy thing is, if the class is not so
complex this will come to the fore during development itself.
But if the class is very complicated, then these kind of errors
will be difficult to track.
• In Windows this will lead to an application popup and unix
will issue a core dump. A careful handling of this will avoid a
lot of nuisance.
• const(Constant) objects and const Member functions
• Keyword const specify that an object is not
modifiable and that any object to modify the object
will result in compilation error
• C++ disallows member functions calls for const
objects unless the member functions themselves are
also declared const
• A constructor must be a non-const member function,
but can still be used to initialize a const object
• “constness” of the const object is enforced from the
time the constructor completes initialization of the
object until object that object’s destructor is called
Destructors

• Destructors are less complicated than


constructors.
• If we don't call them explicitly (they are called
automatically), and there's only one
destructor for each object.
• The name of the destructor is the name of the
class, preceded by a tilde (~).
• A destructor, as the name implies, is used to destroy the
objects that have been created by a constructor.
• A destructor never takes any arguments nor does it return
any value.
• It will be invoked implicitly by the complier upon exit from
the program(or block or function ) to clean up the storage
that is no longer accessible.
• It is a good practice to declare destructors in a program since
it releases memory space for future use.
• Objects are destroyed in the reverse order of creation.
• Destructors receive no parameters and returns no value. Class
may have only one destructor (no overloading of destrcutor is
allowed)
Use of static variable
instead of global variable
in the previous example
Static class members
• a static data member is declared by the word static
• such a member is shared by all the objects of the class – it is
common to all objects of that class
• it exists even when no object of the class has been created
• A static member needs initialization outside the class
• it can be private or public
• private static is accessed through public member functions or
friend functions
• public static is accessed through objects of the class OR
through the class name using scope resolution operator
• Example:
Static member function

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy