Fundament principles of oops Encapsulation
• Encapsulation is defined as binding together the data and the functions that manipulatethem.
Abstraction
• Abstraction means displaying only essential information and hiding the details. Data abstraction
refers to providing only essential information about the data to the outside world, hiding the
background details or implementation.
Polymorphism
• The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. Inheritance
• The capability of a class to derive properties and characteristics from another class is called
Inheritance.
Inheritance:
Allowing a class to inherit properties and methods from another class.
Characteristics of Object-Oriented Languages
• Objects: A programming problem in an object-oriented language, you no longer ask how the
problem will be divided into functions, but how it will be divided into objects. *Object is often
called an “instance” of a class.
• Class: - Class serves as a blueprint of object. - A Class is a user-defined data type that has data
members and member functions
• Inheritance - The concept of classes divided into subclasses. - Subclass shares common
characteristics with the class from which it’s derived. - Vehicle class is divided into cars, trucks,
buses, motorcycles.
• Reusability: -A class has been written, created, and debugged, it can be distributed to other
programmers for use in their own programs. -This is called reusability. -It is similar to the way a
library of functions in a procedural language can be incorporated into different programs
Dynamic Binding
Static Binding: In static binding, function calls are resolved at compile time by the compiler
itself.
Dynamic binding: In dynamic binding, the code to be executed in response to the function call is
decided at runtime
Message Passing
Objects communicate with one another by sending and receiving information.
Structure of C++ Program
• The word main are the distinguishing feature of a function.
• Without the parentheses the compiler would think that main refers to a variable or to some other
program element.
• We’ll see that the parentheses aren’t always empty.
• They’re used to hold function arguments.
• The beginning of a function called main().
• The word int preceding the function name indicates that this particular function has a return value
of type int.
• The body of a function is surrounded by braces (curly brackets {}).
- return the value 0 to whoever called it, in this case the operating system or compiler. • cout - is
actually an object, predefined in <iostream>
• sign (#) - It’s called a preprocessor directive.
• A namespace is a part of the program in which certain names are recognized.
• To avoid adding
- std:: dozens of times in programs we use the using directive instead. Comments are an important
part of any program. They help the person writing a program, and anyone else who must read the
source file, understand what’s going on.
# include<iostream.h> class person P
{
char name[30]; int age;
public: void
getdata(void); void
display(void);
};
void person :: getdata ( void )
{
cout<<”enter name”; cin>>name;
cout<<”enter age”; cin>>age;
}
void display()
{
cout<<”\n name:”<<name;
cout<<”\n age:”<<age;
}
int main( )
{
person p;
p.getdata();
p.display();
return(0);
}
Single Dimensional Array
Single dimensional array (One-dimensional array) is a linear list consisting of related data items of
same data type and in the memory, all the data items are stored contiguously in memory locations one
after the other. Syntax: data_type array_name[size]; Where, data_type: data type can be int, float,
char etc. array_name: name of the array which is a valid C variable. size: it is the number of
elements in the array.
Complete declaration ends with Semicolon.
Ex: int Marks[5];
Declares Marks as an array consisting of 5 elements of integer data type.
Ex: 1. int a[5] = {10, 20, 30, 40, 50};
The compiler allocates 5 memory locations and these locations are initialized with the integer values in
the order specified.
Types of Conversion
1. Implicit Type Conversion :
• Done by the compiler on its own,
without any external trigger
from the user.
• Generally, takes place when in
an expression more than one
data type is present. In such
condition type conversion (type
promotion) takes place to avoid
loss of data.
• All the data types of the
variables are upgraded to the
data type of the variable with
largest data type.
bool -> char -> short int -> int -> unsigned int >
long -> unsigned -> long long -> float -> double
-> long double
• It is possible for implicit conversions to lose information, signs can be lost and overflow can occur
2. Explicit Type Conversion:
• This process is also called type casting and it is user-defined.
• Here the user can typecast the result to make it of a particular data type.
• In C++, it can be done by two ways:
• Converting by assignment: This is done by explicitly defining the required type in front
of
• the expression in parenthesis. This can be also considered as forceful casting.
• Syntax: (type) expression
Constructor and its types
Constructors are special member functions in C++ that are automatically called when an object of a
class is created. They initialize the object and can have different forms based on their functionality.
Short note on Operator Overloading in C++
Operator overloading in C++ allows you to redefine the behavior of operators (e.g., +, -, *, <<, >>, etc.)
for user-defined types like classes. It makes your objects behave more like built-in types and enhances
code readability and usability.
Key Points:
1. Syntax: Use the operator keyword to define an overloaded operator.
2. Types of Operators:
Unary Operators: Operate on a single operand (e.g., -, ++, --).
Binary Operators: Operate on two operands (e.g., +, -, *, /).
3. Rules:
You cannot change the operator's precedence or associativity.
You cannot create new operators.
Some operators, like ::, sizeof, and ?:, cannot be overloaded.
Inline Functions:
An inline function is a function that is expanded in line when it is called. When the inline function is
called whole code of the inline function gets inserted or substituted at the point of the inline
function call. This substitution is performed by the C++ compiler at compile time. An inline
function may increase efficiency if it is small. Syntax: inline return-type function-
name(parameters) { } Features:
1. Defined with the inline keyword: Placed before the function definition.
2.Reduces function call overhead: By embedding the function's code directly at the call site.
3.Used for small functions: Inline functions are most beneficial when the function is small, as large
functions increase code size.
4.Compiler discretion: Even with the inline keyword, the compiler may decide not to make a
function inline, particularly if it is complex or recursive Advantages:
Function call overhead doesn’t occur.
It also saves the overhead of push/pop variables on the stack when a function is called. It
also saves the overhead of a return call from a function.
An inline function may be useful (if it is small) for embedded systems because inline
can yield less code than the function called preamble and return.
Disadvantages:
If you use too many inline functions then the size of the binary executable file will be
large, because of the duplication of the same code.
The inline function may increase compile time overhead if someone changes the code inside the
inline function then all the calling location has to be recompiled because the compiler would be
required to replace all the code once again to reflect the changes, otherwise it will continue with
old functionality.
Function Overloading:
Function overloading is a feature that allows you to define multiple functions with the same name,
but with different parameter lists. The compiler distinguishes these functions by their parameter
signatures, allowing you to call the appropriate function based on the arguments passed. When a
function name is overloaded with different jobs it is called Function Overloading. Example: int
add(int a, int b);
int add(int a, int b, int c);
Characteristics:
1. Same Function Name: The functions share the same name but must differ in: Number of
parameters. Types of parameters. Order of parameters.
2. Compile-Time Polymorphism: Function overloading is an example of compile-time
polymorphism, where the correct function to call is determined during compilation.
Advantages:
1. Improves Readability: Allows the same name for logically related functions.
2. Code Reusability: Eliminates the need to create different function names for similar operations.
3. Flexibility: Provides multiple implementations of the same functionality. Rules for Function
Overloading:
Functions must differ in their parameter list (number, type, or order of parameters). The
return type alone cannot distinguish overloaded functions.
Library Functions
Library functions in C++ are pre-defined functions provided by C++ libraries that perform specific
operations. These functions save time for developers by offering ready-made solutions for common tasks
like input/output operations, mathematical calculations, string manipulations, and more. They are
implemented in header files that need to be included in the program.
Characteristics of Library Functions:
1. Predefined: Already written and compiled, provided with C++ standard libraries.
2. Reusable: Can be directly used in any program by including the appropriate header file.
3. Efficient: Optimized for performance and portability.
4. Object-Oriented Support: Many library functions are part of classes and objects, aligning with
OOP principles.
Common Library Functions and Their Uses:
1. Input/Output Functions (iostream):
Functions like cin, cout, and getline are used for console input and output.
2. Mathematical Functions (cmath):
Functions like sqrt, pow, and abs handle mathematical computations.
3. String Functions (cstring or string):
Functions like strlen, strcat, and substr perform string operations.
4. Standard Template Library (STL):
Provides functions for data structures and algorithms (e.g., sort, find).
Advantages of Library Functions:
1. Saves Time: Eliminates the need to write functions from scratch.
2. Reliability: Tested and optimized for performance.
3. Portability: Works across platforms as part of the standard library.
4. Code Readability: Simplifies code and enhances maintainability.
Friend Function:
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.
Syntax: friend return_type function_name (arguments); where, friend- is a keyword. return_type- is
the type of value the function returns. function_name- is the name given to a function.
arguments- are the input values passed to the function.
The function declaration should be preceded by the keyword friend. The function definition does not
use either the keyword friend or the scope operator ::. The functions that are declared with the
keyword friend are known as friend functions. A function can be declared as a friend in any number of
classes. A friend function, although not a member function, has full rights to the private members of the
class.
Virtual Functions:
A C++ virtual function is a member function in the base class that you redefine in a derived class. It is
declared using the virtual keyword. A 'virtual' is a keyword preceding the normal declaration of a
function. It is used to tell the compiler to perform dynamic linkage or late binding on the function.
When the function is made virtual, C++ determines which function is to be invoked at the runtime
based on the type of the object pointed by the base class pointer.
Rules of Virtual Function: * Virtual functions must be members of some class. * Virtual functions
cannot be static members. * They are accessed through object pointers.
* They can be a friend of another class.
A virtual function must be defined in the base class, even though it is not used. * The prototypes of
a virtual function of the base class and all the derived classes must be identical ** If the two functions
with the same name but different prototypes, C++ will consider them as the overloaded functions
Different types of constructors available in C++:
1. Default Constructor
A constructor that takes no arguments.
Automatically initializes an object with default values.
If no constructor is explicitly defined in a class, the compiler provides a default constructor.
2. Parameterized Constructor
A constructor that takes one or more arguments to initialize an object with specific values. Allows
passing values to the object at the time of creation.
3. Copy Constructor
A constructor that creates a new object as a copy of an existing object.
The compiler provides a default copy constructor, but it can be explicitly defined for custom behavior.
Syntax:
ClassName(const ClassName &obj);
4. Default Argument Constructor
A parameterized constructor with default values for its parameters.
Combines features of both the default and parameterized constructors.
5. Dynamic Constructor
A constructor that allocates memory dynamically using pointers.
Useful for creating dynamic arrays or objects.
6. Destructor Constructor
A destructor is not a constructor but works in tandem with it.
It deallocates memory when an object goes out of scope, complementing dynamic constructors.