C++ First Unit
Unit -1 (Introduction and concept of OOP)
1 . POP & OOP
OOP
OOP, refers to Object Oriented Programming and its deals with objects and their properties. Major concepts of OOPs are −
Class/objects
Abstraction
Encapsulation
Polymorphism
Inheritance
POP
POP, refers to Procedural Oriented Programming and its deals with programs and functions. Programs are divided into functions and data is global.
Following are the important differences between OOP and POP.
2.Basic concept object oriented programming-objects
Object-oriented programming is a type of programming that uses objects and classes its functioning. The object-oriented programming is based on real-world entities like inheritance, polymorphism, data hiding, etc. It aims at binding together data and function work on these data sets into a single entity to restrict their usage.
Some basic concepts of object-oriented programming are −
CLASS
OBJECTS
ENCAPSULATION
POLYMORPHISM
INHERITANCE
ABSTRACTION
Class: A class is a data type that has its own members i.e. data members and member functions. It is the blueprint for an object in an object-oriented programming language. It is the basic building block of object-oriented programming in C++. The members of a class are accessed in a programming language by creating an instance of the class.
Some important properties of the class are −
Class is a user-defined data type.
A class contains members like data members and member functions.
Data members are variables of the class.
Member functions are the methods that are used to manipulate data members.
Data members define the properties of the class whereas the member functions define the behavior of the class.
A class can have multiple objects which have properties and behavior that in common for all of them.
Data Abstraction & Encapsulation
Data Abstraction: Data abstraction refers to providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in the program without presenting the details.
For example, your program can make a call to the sort() function without knowing what algorithm the function actually uses to sort the given values.
#include <iostream>
using namespace std;
int main() {
cout << "Hello C++" <<endl;
return 0;
}
Here, you don't need to understand how cout displays the text on the user's screen. You need to only know the public interface and the underlying implementation of ‘cout’ is free to change.
Encapsulation: Encapsulation in C++ is defined as the wrapping up of data and information in a single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
Example:
#include <iostream>
using namespace std;
class temp{
int a;
int b;
public:
int solve(int input){
a=input;
b=a/2;
return b;
}
};
int main() {
int n;
cin>>n;
temp half;
int ans=half.solve(n);
cout<<ans<<endl;
}
Inheritance :
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class.
When we say derived class inherits the base class, it means, the derived class inherits all the properties of the base class, without changing the properties of base class and may add new features to its own. These new features in the derived class will not affect the base class. The derived class is the specialized class for the base class.
Sub Class: The class that inherits properties from another class is called Subclass or Derived Class.
Super Class: The class whose properties are inherited by a subclass is called Base Class or Superclass.
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. A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So the same person exhibits different behavior in different situations. This is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented Programming.
This type of polymorphism is achieved by function overloading or operator overloading.
Function Overloading
Operator Overloading
1. Function Overloading:- When there are multiple functions with the same name but different parameters, then the functions are said to be overloaded, hence this is known as Function Overloading. Functions can be overloaded by changing the number of arguments or/and changing the type of arguments.
Function add(int a , int b){}
Function add(int a , int b , int c){}
Function add(int a , int b , int c , int d){}
2 . Operator Overloading: - C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can make use of the addition operator (+) for string class to concatenate two strings. We know that the task of this operator is to add two operands. So a single operator ‘+’, when placed between integer operands, adds them and when placed between string operands, concatenates them.
Dynamic Binding:-
Dynamic binding is used on a specified call and dynamically identified at runtime. We use Dynamic binding in C++ because it is flexible as a single function can handle multiple objects at runtime.
dynamic binding is delaying the selection or choice of which function to run until its runtime.
In other words, dynamic binding simply means selecting a particular function to run until the runtime. According to the type of object, the respective function will be called.
Dynamic binding in C++ takes place at runtime. Dynamic Binding is known as late Binding.In Dynamic Binding, function calls are not resolved until runtime.
Message Passing:-
The object-oriented programming (oops) concepts in C++ involves the use of objects which are real-life instances of classes. Message passing in C++ is the communication between two or more objects using a logical entity called a message.
What is a Message?
A message is a form of request that is made to an object to perform a function specific to the object to which the request was made.
A message is a form of communication between objects that make an object perform a task for the system.
The message is an abstract entity and will not hold the information of the function that is going to be performed and only plays the role of invoking the function.
Example :
Employee.salary(Name);
This will return employee salary
Benefits of OOP
Users can create new data types or user can define data types by making class
Code can be reused by using inheritance
Data can be hidden from the outside world by using encapsulation
Operators and functions can be overloaded by using polymorphism, so the same functions or operators can be used for multitasking
The object-oriented system can be easily upgraded from small system to large system
It is easy to partition work for the same project
Message-passing techniques make communication easier
Software complexity can be easily managed
Maintenance cost is less
It is simple to implement
Application of OOPs:
Real-Time System design
Stimulation and modeling system
Object-oriented database
Hypertext and Hypermedia
AI Expert System
Client-server system
Office Automation System
CIM - Computer Integrated Manufacturing
CAM – Computer-Aided Manufacturing
CAD – Computer-Aided Design System
What is C++?
C++ is a cross-platform language that can be used to create high-performance applications
C++ is an object-oriented programming language.
C++ was developed by Bjarne Stroustrup, At Bell Laboratories in Murray hill, new jersey, USA, in the year 1980.
Structure of a C++ Program:
Programs refer to a sequence of instructions or statements. These statements are what form the structure of a C++ program. Moreover, the C++ program structure divides into several sections which are namely headers, class definitions, member functions definitions, and main functions.
Headers: a program includes different programming elements such as built-in functions, classes, keywords, constants, operators and more which are already defined in the standard C++ library.
Example: #include<iostream.h>
Class Declaration:- In this declaration section, we declare the class and the class contains variables & functions.
Member Functions Definition:- In this section, you can declare the function as well as execution(body part)
Main Function Program:-
The main() is a start-up function that starts the execution of a C++ program.
All C++ statements that need to be executed are written within main().
Here we can create an object of class also
Input Output Operators in C++
Output Operator: The operator << is called insertion operator and used with cout.
Ex: Cout <<”C++ is better then C”;
Input Operator: The operator >> is called extraction operator and used with cin.
Ex: cin>>number1;
Cascading of I\O Operator:- The multi-use of << (Insertion) and >> (Extraction) in one statement is called cascading.
Ex:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
Void main(){
Int x,y;
Clrscr();
cout<<”Enter Any Two number”;
cin>>x>>y;
cout<<”Your Value is :”<<x<<” and ”<<y;
getch();
}
Keywords, Data types (Basic, Users-Defined, Derived), Variables - Reference, Control Structure
Keywords:-Keywords are those words whose meaning is already defined by the compiler. These keywords cannot be used as an identifier. Keywords are the collection of reserved words and predefined identifiers.
Data Types in C++:
User Define: Structure, Union, Enum, Class
Built-in: int, char, float, double, Boolean, void
Derived: Array, Function, Pointer Reference
Reference Variable: A Reference Variable is an alias that is another name for an already existing variable. Once a reference is initialized with a variable, whether that variable name or the reference name may be used to refer to the variable
Comments
Post a Comment