0% found this document useful (0 votes)
11 views

OOP Unit 4

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

OOP Unit 4

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

UNIT-4

C++ Basics: Overview


Syntax and Structure of C++ program

Here we will discuss one simple and basic C++ program to print "Hello this is C++"
and its structure in parts with details and uses.

First C++ program

#include <iostream.h>
using namespace std;
int main()
{
cout << "Hello this is C++";
}

Header files are included at the beginning just like in C program. Here iostream is a
header file which provides us with input & output streams. Header files contained
predeclared function libraries, which can be used by users for their ease.

Using namespace std, tells the compiler to use standard namespace. Namespace
collects identifiers used for class, object and variables. NameSpace can be used by
two ways in a program, either by the use of using statement at the beginning, like we
did in above mentioned program or by using name of namespace as prefix before the
identifier with scope resolution (::) operator.

Example: std::cout << "A";

Enum as Datatype in C++

Enumerated type declares a new type-name along with a sequence of values


containing identifiers which has values starting from 0 and incrementing by 1 every
time.
Typecasting

Type casting refers to the conversion of one data type to another in a program.
Typecasting can be done in two ways: automatically by the compiler and manually by
the programmer or user. Type Casting is also known as Type Conversion.

Type Casting is divided into two types: Implicit conversion or Implicit Type Casting
and Explicit Type Conversion or Explicit Type Casting.

For example,

1. int num = 5;
2. float x;
3. x = float(num);
4. x = 5.0
Pass By reference;

Return by reference in C++ with Examples

Return by reference is very different from Call by reference. Functions behaves a


very important role when variable or pointers are returned as reference.

#include <iostream>
using namespace std;
int x;

int& retByRef()
{
return x;
}

int main()
{
retByRef() = 10;
cout << x;
return 0;
}
Inline functions,

An inline function is a function that is expanded in line when it is invoked. Inline


expansion
makes a program run faster because the overhead of a function call and return is
eliminated. It
is defined by using key word “inline”

syntax:

inline return-type function-name(parameters)


{
// function code
}
Examples of Inline Functions in C++

#include <iostream>

using namespace std;

inline int cube(int s) // inline function

{ return s * s * s;

}
int main()
{ cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}

What are Macros?

Macros are defined using the #define directive. They provide a way to create
symbolic constants and code snippets that can be reused throughout a program.

Syntax

#define MACRO_NAME macro_definition


Difference between Inline and Macro in C++ :

S.NO Inline Macro

An inline function is defined by Whereas the macros are defined by


1.
the inline keyword. the #define keyword.

Through inline function, the class’s Whereas macro can’t access the class’s
2.
data members can be accessed. data members.

In the case of inline function, the Whereas in the case of macros, the
3.
program can be easily debugged. program can’t be easily debugged.

Whereas in the case of macro, the


In the case of inline, the arguments
4. arguments are evaluated every time
are evaluated only once.
whenever macro is used in the program.

In C++, inline may be defined either Whereas the macro is all the time defined
5.
inside the class or outside the class. at the beginning of the program.

In C++, inside the class, the short


6. length functions are automatically While the macro is specifically defined.
made the inline functions.

Inline is not as widely used as


7. While the macro is widely used.
macros.

Inline is not used in competitive While the macro is very much used in
8.
programming. competitive programming.

Inline function is terminated by the While the macro is not terminated by any
9.
curly brace at the end. symbol, it is terminated by a new line.
Friend functions:-

A friend function is a function which is declared within a class and is defined outside
the class. It
does not require any scope resolution operator for defining . It can access private
members of a class. It is declared by using keyword “friend”.

Declaration of friend function in C++

class class_name
{
friend data_type function_name(argument/s); // syntax of friend function.

};

Example:
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. private:
6. int length;
7. public:
8. Box(): length(0) { }
9. friend int printLength(Box); //friend function
10. };
11. int printLength(Box b)
12. {
13. b.length += 10;
14. return b.length;
15. }
16. int main()
17. {
18. Box b;
19. cout<<"Length of box: "<< printLength(b)<<endl;
20. return 0;
21. }
Virtual Function

Virtual Function

Virtual function is a member function that is declared within the base class and can
be redefined by the derived class.

#include <iostream>
using namespace std;

class base {
public:
virtual void print()
{ cout << "print base class\n"; }

void show() { cout << "show base class\n"; }


};

class derived : public base {


public:
void print() { cout << "print derived class\n"; }

void show() { cout << "show derived class\n"; }


};

int main()
{
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();

return 0;
}

Pure Virtual Functions:


A pure virtual function is a virtual function in C++ for which we need not to write any
function definition and only we have to declare it. It is declared by assigning 0 in the
declaration.

An abstract class is a class in C++ which have at least one pure virtual function.

Syntax:

class Test
{

public:
virtual void show() = 0; // Pure Virtual Function
};

Example of Pure Virtual Functions

#include<iostream>
using namespace std;
class B {
public:
virtual void s() = 0; // Pure Virtual Function
};

class D:public B {
public:
void s() {
cout << "Virtual Function in Derived class\n";
}
};

int main() {
B *b;
D dobj;
b = &dobj;
b->s();
}
Virtual function Pure virtual function

A virtual function is a member function in a A pure virtual function is a member function in


base class that can be redefined in a derived a base class whose declaration is provided in a
class. base class and implemented in a derived class.

The classes which are containing virtual The classes which are containing pure virtual
functions are not abstract classes. function are the abstract classes.

In case of a virtual function, definition of a In case of a pure virtual function, definition of


function is provided in the base class. a function is not provided in the base class.

The base class that contains a virtual function The base class that contains a pure virtual
can be instantiated. function becomes an abstract class, and that
cannot be instantiated.

If the derived class will not redefine the virtual If the derived class does not define the pure
function of the base class, then there will be no virtual function; it will not throw any error but
effect on the compilation. the derived class becomes an abstract class.

All the derived classes may or may not All the derived classes must define the pure
redefine the virtual function. virtual function.
Function Overloading: When there are multiple functions with same name but
different parameters then these functions are said to be overloaded. Functions can be
overloaded by change in number of arguments or/and change in type of arguments.

Example: // C++ program for function overloading


Important questions of unit-4

1. Describe the term enum.


2. Define friend function with example.
3. Explain the use of typecasting?
4. What is the difference between reference and pointer?
5. Differentiate Macro and Inline functions.
6. What is the namespace in C++? Explain its significance.
7.
8.
9. Describe the concept of typecasting in C++.
10.Explain the difference between call by value and call by reference in
C++, with the help of suitable example.
11.What are the differences between a friend function for a class and a
member function of a class ? Explain with the help of a example.
12.Differentiate Function overloading VS Operator Overloading
13.What is the difference between reference and pointer?
14.Differentiate Macro and Inline functions.
15.Write short notes any two of the following :
(a) OperatorOverloading in C++
(b) Dynamic Models
(c) Virtual functions in C++.

You might also like