Professional Documents
Culture Documents
03 - C++ Notes PDF
03 - C++ Notes PDF
7. In the above example both the Local and Global variables have different values, helping to identify
the version accessed while printing.
1
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
5. ReturnType: This will suggest what the function will return. It can be int, char, some pointer or
even a class object. There can be functions which do not return anything, they are mentioned
using void.
6. FunctionName: the functions are called / invoked using FunctionName.
7. Parameters: These are variables to hold values of arguments passed while a function is called. A
function may or may not contain a parameter list.
2
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
3
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
Passing arguments to a function has two methods: (A) Call by Value and (B) Call by Reference
6. This shows that there is no change in the values though they had been changed inside the function.
4
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
1. This method copies the reference of an argument into the formal parameter.
2. Inside the function, the reference is used to access the actual argument used in the call. This means
that changes made to the parameter affects the arguments passed.
3. A reference provides an alias (an alternate name) for the variable.
4. To pass the value by reference, argument reference is passed to the functions just like any other
value.
5. So to declare the function parameters as reference types as in the following function
Swap (), which exchanges the values of the two integer variables pointed to by its arguments.
6. The following example displays use of Call by Reference.
SOURCE CODE: (Use of Call by Reference)
5
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
5. Objects Creation: A member function will be called using a dot operator [ . ] on an object where it
will manipulate data related to that object only as follows:
6
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
7
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
1. A default constructor is a constructor which can be called with no arguments (either defined with
an empty parameter list, or with default arguments provided for every parameter)
2. The default constructors are called during default initializations and value initializations.
1. A default constructor does not have any parameter, but if required, a constructor can have
parameters.
2. This helps to assign initial value to an object at the time of its creation.
8
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
2. If a copy constructor is not defined in a class, the compiler itself defines one.
3. If the class has pointer variables and has some dynamic memory allocations, then it is a must to
have a copy constructor. The most common form of copy constructor is shown in the below
example.
6. Destructors
1. A destructor is a special member function of a class that is executed whenever an object of its class
goes out of scope.
2. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither
return a value nor can it take any parameters. Destructor can be very useful for releasing
resources before coming out of the program like closing files, releasing memories etc.
9
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
10
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
11
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
5. Syntax:
12
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
6. Here, friend function func() is declared inside Distance class. So, the private data can be accessed
from this function.
13
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
2. The unary operators operate on the object for which they were called and normally, this operator
appears on the left side of the object, example !obj, -obj, and ++obj but sometime they can be used
as postfix as well like obj++ or obj--.Following example explains how minus (-) operator can be
overloaded for prefix as well as postfix usage.
14
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
2. Following example explains how addition (+) operator can be overloaded. Similar way, you can
overload subtraction (-) and division (/) operators.
15
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
16
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
17
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
9. Inheritance
1. Inheritance is one of the key features of object-oriented programming including C++ which allows
user to create a new class (derived class) from a existing class (base class).
2. The derived class inherits all features from a base class and it can have additional features of its
own.
3. Inheritance allows us to define a class in terms of another class, which makes it easier to create
and maintain an application. This also provides an opportunity to reuse the code functionality and
fast implementation time.
4. When creating a class, instead of writing
completely new data members and member
functions, the programmer can designate that the
new class should inherit the members of an
existing class.
5. This existing class is called the base class, and the
new class is referred to as the derived class.
6. The inheritance relationship enables a derived
class to inherit features from its base class.
7. Furthermore, the derived class can add new
features of its own. Therefore, rather than create
completely new classes from scratch, you can take
advantage of inheritance and reduce software
complexity.
18
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
4. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class
for other classes.
5. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other
four types of inheritance.
1. A class can be derived from more than one class, which means it can inherit data and functions
from multiple base classes. To define a derived class, we use a class derivation list to specify the
base class.
2. A class derivation list names one or more base classes and has the form:
3. Where access-specifier is one of public, protected, or private, and base-class is the name of a
previously defined class.
5. Consider a base class Shape and its derived class Rectangle as follows:
19
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
2. Here, access is one of public, protected, or private and would be given for every base class and
they will be separated by comma as shown above.
3. Let us try the following example:
20
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
21
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
22
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
23
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
9. b. Types of Inheritance
Member Access
Specifiers Private Members of Base Protected members of base Public members of base
Protected
Inaccessible Protected Protected
Inheritance
NOTE:
In principle, a derived class inherits every member of a base class except constructor and destructor. It
means private members are also become members of derived class. But they are inaccessible by the
members of derived class.
24
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
10. Polymorphism
1. The word polymorphism means having many forms. Typically, polymorphism occurs when there
is a hierarchy of classes and they are related by inheritance.
2. C++ polymorphism means that a call to a member function will cause a different function to be
executed depending on the type of object that calls the function.
3. Consider the following example where a base class has been derived by other two classes:
25
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
4. When the above code is compiled and executed, it produces the following result:
26
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
9. This time, the compiler looks at the contents of the pointer instead of its type.
10. Since addresses of objects of tri and rec classes are stored in *shape the respective area () function
is called.
11. As you can see, each of the child classes has a separate implementation for the function area ().
12. This is how polymorphism is generally used. You have different classes with a function of the same
name, and even the same parameters, but with different implementations.
1. A virtual function is a function in a base class that is declared using the keyword virtual. Defining
in a base class a virtual function, with another version in a derived class, signals to the compiler
that we don't want static linkage for this function.
2. What we do want is the selection of the function to be called at any given point in the program to
be based on the kind of object for which it is called.
3. This sort of operation is referred to as dynamic linkage or late binding.
1. It's possible that you'd want to include a virtual function in a base class so that it may be redefined
in a derived class to suit the objects of that class, but that there is no meaningful definition you
could give for the function in the base class.
2. We can change the virtual function area() in the base class to the following:
27
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
28
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
1. Before concluding on virtual functions and run-time polymorphism, there are two terms that need
to be defined because they are used frequently in discussions of C++ and object-oriented
programming: early binding and late binding.
2. Early binding refers to events that occur at compile time. In essence, early binding occurs when
all information needed to call a function is known at compile time. (Put differently, early binding
means that an object and a function call are bound during compilation.)
3. Examples of early binding include normal function calls (including standard library functions),
overloaded function calls, and overloaded operators.
4. The main advantage to early binding is efficiency. Because all information necessary to call a
function is determined at compile time, these types of function calls are very fast.
5. Late binding is the opposite of early binding. As it relates to C++, late binding refers to function
calls that are not resolved until run time.
6. Virtual functions are used to achieve late binding. As you know, when access is via a base pointer
or reference, the virtual function actually called is determined by the type of object pointed to by
the pointer.
7. Because in most cases this cannot be determined at compile time, the object and the function are
not linked until run time. The main advantage to late binding is flexibility.
8. Unlike early binding, late binding allows you to create programs that can respond to events
occurring while the program executes without having to create a large amount of "contingency
code".
9. Keep in mind that because a function call is not resolved until run time, late binding can make for
somewhat slower execution times.
29
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
1. Before beginning our discussion of the C++ file system, it is necessary to know the difference
between the terms streams and files.
2. The C++ I/O system supplies a consistent interface to the programmer independent of the actual
device being accessed. That is, the C++ I/O system provides a level of abstraction between the
programmer and the device.
3. This abstraction is called a stream and the actual device is called a file. It is important to
understand how streams and files interact.
11(A) Streams:
1. The C++ file system is designed to work with a wide variety of devices, including terminals, disk
drives, and tape drives.
2. Even though each device is very different, the buffered file system transforms each into a logical
device called a stream.
3. All streams behave similarly. Because streams are largely device independent, the same function
that can write to a disk file can also be used to write to another type of device, such as the console.
4. There are two types of streams: text and binary.
5. Text file: It is a file that stores information in ASCII characters. In text files, each line of text is
terminated with a special character known as EOL (End of Line) character or delimiter character.
When this EOL character is read or written, certain internal translations take place.
6. Binary file: It is a file that contains information in the same format as it is held in memory. In
binary files, no delimiters are used for a line and no translations occur here.
11(B) Files:
1. In C++, a file may be anything from a disk file to a terminal or printer.
2. You associate a stream with a specific file by performing an open operation.
3. Once a file is open, information may be exchanged between it and your program.
4. Not all files have the same capabilities. For example, a disk file can support random access while
some printers cannot. This brings up an important point about the C++ I/O system: All streams are
the same but all files are not.
30
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
6. This requires another standard C++ library called fstream, which defines three new data types:
This data type represents the output file stream and is used to create files and to
ofstream
write information to files.
This data type represents the input file stream and is used to read information from
ifstream
files.
This data type represents the file stream generally, and has the capabilities of both
fstream ofstream and ifstream which means it can create files, write information to files,
and read information from files.
Opening a File:
1. A file must be opened before you can read from it or write to it.
2. Either the ofstream or fstream object may be used to open a file for writing.
3. The ifstream object is used to open a file for reading purpose only.
4. Following is the standard syntax for open () function, which is a member of fstream, ifstream, and
ofstream objects.
5. Here, the first argument specifies the name and location of the file to be opened and the second
argument of the open () member function defines the mode in which the file should be opened.
Mode Flag Description
ios::app Append mode. All output to that file to be appended to the end.
Ios::ate Open a file for output and move the read/write control to the end of the file.
Ios::trunc If the file already exists, its contents will be truncated before opening the file.
6. You can combine two or more of these values by OR-ing them together.
31
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
7. For example if you want to open a file in write mode and want to truncate it in case it already
exists, following will be the syntax:
ofstream outfile;
outfile.open (“file.dat”, ios::out | ios::in);
8. In a similar way, you can open a file for reading and writing purpose as follows:
fstream afile;
afile.open (“file.dat”, ios::out | ios::in);
Closing a File:
1. When a C++ program terminates it automatically closes flushes all the streams, release all the
allocated memory and close all the opened files.
2. But it is always a good practice that a programmer should close all the opened files before
program termination.
3. Following is the standard syntax for close () function, which is a member of fstream, ifstream, and
ofstream objects.
Void close();
Writing to a File:
1. While doing C++ programming, you write information to a file from your program using the
stream insertion operator (<<) just as you use that operator to output information to the screen.
2. The only difference is that you use an ofstream or fstream object instead of the cout object.
1. You read information from a file into your program using the stream extraction operator (>>) just
as you use that operator to input information from the keyboard.
2. The only difference is that you use an ifstream or fstream object instead of the cin object.
32
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
2. Above examples make use of additional functions from cin object, like getline () function to read
the line from outside and ignore () function to ignore the extra characters left by previous read
statement.
33
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
eof() returns true (non zero) if end of file is encountered while reading; otherwise return false(zero)
34
COMPILED BY – NISHANT KAWA
Object Oriented Programming with C++
7. The refposition takes one of the following three constants defined in the ios class.
35
COMPILED BY – NISHANT KAWA