OOP Unit 4
OOP Unit 4
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.
#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.
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;
#include <iostream>
using namespace std;
int x;
int& retByRef()
{
return x;
}
int main()
{
retByRef() = 10;
cout << x;
return 0;
}
Inline functions,
syntax:
#include <iostream>
{ return s * s * s;
}
int main()
{ cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
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
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.
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.
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”.
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"; }
int main()
{
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
}
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
};
#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
The classes which are containing virtual The classes which are containing pure virtual
functions are not abstract classes. function are the abstract classes.
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.