0% found this document useful (0 votes)
24 views7 pages

C++ 1

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

C++ 1

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

LECTURE 1

Introduction to Object-Oriented Design


Object-oriented programming (OOP) is a computer programming model that
organizes software design around data, or objects, rather than functions and logic.
An object can be defined as a data field that has unique attributes and behavior.

OOP provides a clear structure for the programs. OOP helps to keep the C++ code
DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and
debug. OOP makes it possible to create full reusable applications with less code
and shorter development time.

Objective of OOP
C++ is a programming language with a special focus on the concepts of OOP and
their implementation. It has object-oriented features, which allow the programmer
to create objects within the code. This makes programming easier, more efficient,
and some would even say more fun.

What is a Class in C++?


A class is a user-defined data type, which holds its own data members and
member functions, which can be accessed and used by creating an instance of that
class. A C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with
different names and brands but all of them will share some common properties
like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, the
Car is the class, and wheels, speed limits, and mileage are their properties.
 A Class is a user-defined data type that has data members and member
functions.
 Data members are the data variables and member functions are the functions
used to manipulate these variables together, these data members and member
functions define the properties and behavior of the objects in a Class.
 In the above example of class Car, the data member will be speed
limit, mileage, etc., and member functions can be applying brakes, increasing
speed, etc.
But we cannot use the class as it is. We first have to create an object of the class
to use its features. An Object is an instance of a Class.
E

What is an Object in C++?


When a class is defined, only the specification for the object is defined; no
memory or storage is allocated. To use the data and access functions defined in
the class, you need to create objects.
Syntax to Create an Object
We can create an object of the given class in the same way we declare the
variables of any other inbuilt data type.
ClassName ObjectName;
Constructors
Constructor is special class members which are called by the compiler every
time an object of that class is instantiated. Constructors have the same name
as the class and may be defined inside or outside the class definition.
There are 4 types of constructors in C++ classes:
 Default constructor: The constructor that takes no argument is
called default constructor.
 Parameterized Constructors: This type of constructor takes the
arguments to initialize the data members.

 Copy Constructor creates the object from an already existing object


by copying it.
 Move constructor the move constructor also creates the object
from an already existing object but by moving it.
Note: If the programmer does not define the constructor, the compiler
automatically creates the default, copy and move constructor.

. It deallocates all the memory previously used by the object of the class so
that there will be no memory leaks. The destructor also has the same name
as the class but with tilde (~) as prefix.
Difference between keywords and identifiers.

Identifiers are the values used to


Keywords are predefined word
define different programming
that gets reserved for working
items such as variables, integers,
1 program that have special
structures, unions and others
meaning and cannot get used
and mostly have an alphabetic
anywhere else.
character.

Identify the name of a particular


2 Specify the type/kind of entity.
entity.

First character can be a


It always starts with a lowercase
3 uppercase, lowercase letter or
letter.
underscore.

A keyword should be in lower An identifier can be in upper


4
case. case or lower case.

An identifier can consist of


A keyword contains only
5 alphabetical characters, digits
alphabetical characters.
and underscores.

They help to identify a specific They help to locate the name of


6 property that exists within a the entity that gets defined along
computer language. with a keyword.

No special symbol, punctuation is No punctuation or special symbol


7
used. except ‘underscore’ is used.

Examples of keywords are: int, Examples of identifiers are: Test,


8
char, if, while, do, class etc. count1, high_speed, etc.

Destructor
Destructor is an instance member function that is invoked automatically
whenever an object is going to be destroyed. Meaning, a destructor is the
last function that is going to be called before an object is destroyed.
In this article, we will learn about the destructors in C++, how they work, how
and why to create the user defined destructors with the code examples. At
the end, we will look at some commonly used questions about C++
destructors.
Characteristics of a Destructor
 A destructor is also a special member function like a constructor.
Destructor destroys the class objects created by the constructor.
 Destructor has the same name as their class name preceded by a tilde
(~) symbol.
 It is not possible to define more than one destructor.
 The destructor is only one way to destroy the object created by the
constructor. Hence, destructor cannot be overloaded.
 It cannot be declared static or const.
 Destructor neither requires any argument nor returns any value.
 It is automatically called when an object goes out of scope.
 Destructor release memory space occupied by the objects created by the
constructor.
 In destructor, objects are destroyed in the reverse of an object creation.
The thing is to be noted here if the object is created by using new or the
constructor uses new to allocate memory that resides in the heap memory or
the free store, the destructor should use delete to free the memory.
Static data types
Static data members are class members that are declared
using static keywords. A static member has certain special characteristics
which are as follows:
 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 initialized before any object of this class is created, even before the
main starts outside the class itself.
 It is visible can be controlled with the class access specifiers.
 Its lifetime is the entire program.

Friend Class and Function in C++


A friend class can access private and protected members of other


classes in which it is declared as a friend. It is sometimes useful to
allow a particular class to access private and protected members of
other classes. For example, a Linked List class may be allowed to
access private members of Node.
We can declare a friend class in C++ by using the friend keyword.
Syntax:
friend class class name; // declared in the base class

Note: We can declare friend class or function anywhere in the base


class body whether its private, protected or public block. It works all
the same.
Friend Function
Like a friend class, a friend function can be granted special access to
private and protected members of a class in C++. They are not the
member functions of the class but can access and manipulate the
private and protected members of that class for they are declared
as friends.
A friend function can be:
1. A global function
2. A member function of another class

Friend Function in C++


1. Global Function as Friend Function
We can declare any global function as a friend function. The following example
demonstrates how to declare a global function as a friend function in C++:
.
Features of Friend Functions
 A friend function is a special function in C++ that in spite of not being a member
function of a class has the privilege to access the private and protected data of a
class.
 A friend function is a non-member function or ordinary function of a class, which
is declared as a friend using the keyword “friend” inside the class. By declaring a
function as a friend, all the access permissions are given to the function.
 The keyword “friend” is placed only in the function declaration of the friend
function and not in the function definition or call.
 A friend function is called like an ordinary function. It cannot be called using the
object name and dot operator. However, it may accept the object as an argument
whose value it wants to access.
 A friend function can be declared in any section of the class i.e. public or private
or protected.
Below are some more examples of friend functions in different scenarios:
The friend function provides us with a way to access private data but it also has its
demerits. Following is the list of advantages and disadvantages of friend functions in
C++:
Advantages of Friend Functions
 A friend function is able to access members without the need of inheriting the
class.
 The friend function acts as a bridge between two classes by accessing their private
data.
 It can be used to increase the versatility of overloaded operators.
 It can be declared either in the public or private or protected part of the class.
Disadvantages of Friend Functions
 Friend functions have access to private members of a class from outside the class
which violates the law of data hiding.
 Friend functions cannot do any run-time polymorphism in their members.
Important Points about Friend Functions and Classes
1. Friends should be used only for limited purposes. Too many functions or external
classes are declared as friends of a class with protected or private data access
lessen the value of encapsulation of separate classes in object-oriented
programming.
2. Friendship is not mutual. If class A is a friend of B, then B doesn’t become a
friend of A automatically.
3. Friendship is not inherited.
4. The concept of friends is not in Java.

You might also like