Object-oriented programming has gained some fame over the last few years. Its code flexibility and effective problem-solving nature are only two of the reasons for its sudden peak. Developers all-round consider OOP to be an important skill to have.
3. Contents
Pointers
Structures
Objects and Classes
Conversion between basic and user-defined data types
Functions and Types
Encapsulation
Inheritance
Polymorphism
Unary and Binary Operator’s Overloading
File Handling in C++
5. Pointers
A pointer is a variable that stores the memory address as its value.
A pointer variable points to a data type (like int or string) of the same
type, and is created with the * operator. The address of the variable you're
working with is assigned to the pointer:
For Example:
string food = "Pizza"; // A food variable of type string
string* ptr = &food; //A pointer variable, with the name ptr, that stores the
address of food
7. Let’s See Code
#include<iostream>
#include<conio.h>
using namespace std;
void sum(int m, int n);
int main(){
int m = 10;
int n = 2
sum(m,n);
cout<<m;
}
void sum(int m, int n){
m= m+n;
}
9. To overcome this issue
We make a pointer that holds the address of a variable
Any Changes in a reference variable are non-volatile
There are no way of passing a variable "by reference" to a function. That's where
you have to use pointers
11. Structures
We often come around situations where we need to store a group of data whether
of similar data types or non-similar data types. We have seen Arrays in C++ which
are used to store set of data of similar data types
A structure is a user-defined data type in C/C++. A structure creates a data type
that can be used to group items of possibly different types into a single type.
13. OOP
Class and Objects
Encapsulation
Polymorphism
Inheritance
14. (OOP)
Objects and Classes
A class in C++ is a user-defined type or data structure declared with keyword class
that has data and functions as its members whose access is governed by the three
access specifiers private, protected or public. By default access to members of a C++
class is private.
Class is a blueprint, Object is physically present in memory
18. Functions and Types
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function
C++ provides some pre-defined functions, such as main(), which is
used to execute code. But you can also create your own functions to
perform certain actions.
Syntax
void myFunction() {
// code to be executed
}
void means that the function does not have a return value.
20. Types of Function
Return types can be bool, char, string, int, float even an object
Int type
Bool type
Both are Pass-by Value methods
21. Pass by Value vs. Pass by Reference
In call by reference the actual value that is passed as argument is changed after
performing some operation on it. When call by reference is used, it creates a copy
of the reference of that variable into the stack section in memory
The call by reference is mainly used when we want to change the value of the
passed argument into the invoker function.
24. Why Encapsulation?
Consider a real life example of encapsulation, in a company there are different
sections like the accounts section, finance section, sales section etc. The finance
section handles all the financial transactions and keep records of all the data
related to finance. Similarly the sales section handles all the sales related activities
and keep records of all the sales. Now there may arise a situation when for some
reason an official from finance section needs all the data about sales in a particular
month. In this case, he is not allowed to directly access the data of sales section. He
will first have to contact some other officer in the sales section and then request
him to give the particular data. This is what encapsulation is. Here the data of sales
section and the employees that can manipulate them are wrapped under a single
name “sales section”.
39. File Handling
Files are important as they store our data
#include<fstream>
Creating a file: open()
Reading data: read()
Writing new Data: write()
Closing a file (crucial): close()