Object Oriented Programming Language (OOPL)
Object-oriented programming or OOP is a methodology or a paradigm to design a program using classes and objects utilising features of OOPs such as abstraction, encapsulation, inheritance and polymorphism.
Features of (OOPL)
The main motivating factor of object oriented paradigm is to remove those problems arouse in procedural oriented programming. There are four pillars of object oriented programing:
i. Encapsulation:
It is the property of wrapping data and member functions in a single entity. This property is achieved by class and prevents from outside interference. Encapsulation helps us to achieve data abstraction.
It is an extended form of encapsulation. Abstraction means hiding of data and information. Only the necessary features of an entity are shown to the outside world and the irrelevant informations are hidden. For example, to get any information, we just need to enter the domain name in the search bar of a browser, say google chrome, without concerning of how google serves those informations to us.
It is the property of creating a new class from an existing class. It allows the new class to acquire the properties of the existing class. Hence, it provides code reusability. Once written, we can reuse the codes again and again just by defining it only once. For example, a child inherits properties from parent.
iv. Polymorphism:
As the name suggests, polymorphism means which has many forms i.e. an entity can behave differently in different situations. For example, a girl is a student at school, and at home, she is a daughter.
Classes: A class is a user-defined data type which consists of data members and member functions.
Data members are the variables used in a program and member functions are the functions through which the variables get accessed.
Objects: An object is an instance of a class. It is also called as runtime entity. It contains all the properties of a class.
Example:
class C //class 'C'
{
//data members
//member functions
}
C obj; //object 'obj' of class C
Input and output statement:
In C++, the input and output tasks are carried out by 2 identifiers.
i. cout<< :-
The cout identifier along with << (insertion operator) is used to display message on the standard output device (screen).
Example: cout<<"Smash";
ii. cin>> :-
The cin identifier along with >> ( extraction operator) is used to take input from standard input device (keyboard).
Example: cin>>x;
Comments:
Comments (//) are non-executable statements of a program. Single line comment starts with \\. Multiline comments start with /* and end with */.
using namespace std: Namespace, a feature in C++ (not in C), is a limited region which provides scope to the identifiers ( functions, variables etc). std is the standard namespace in C++. It differentiate similar names.
Structure of a C++ program
The basic structure of a C++ program contains four sections.
i. Include section:
It links all the library files required in a program. It contains pre-processor directories and the header file required to run the program.
Example: #include<iostream.h>
Here, the input/output stream is included which contains the definition of i/o built-in functions and objects like cout, cin.
ii. Class declaration:
This section defines the class. A class is an user defined data type which consists of two parts.
- Data Member
- Member Function
Example: class employee
{
int id; //data member
public:
//member function
void getdata();
void putdata();
iii. Member Function Definition:
It includes descriptive statements which explain the performance of function. It also explain the sequence in which the program gets executed.
iv. Main Function: It is the compulsory section of any program. There is only one main function in each program because the execution of any program begins from main. It has int return type by default.
Example:
int main() //main function
{
student s;
s.getdata();
s.putdata();
}
Data Type in C++
Datatype: It is description of the type of variable we use in a program to restrict its usage.
Data types are of three types:
i. Primitive (basic) datatype: These are built-in or pre-defined types.These are building blocks of a program. They are directly used to declare variables. Example int, char, float, double etc.
ii. Derived Data Types: These are derived from the built-in datatypes. Example array, pointer etc.
iii. Abstract (user-defined) Data Types: These are defined by user itself. Example class, structure etc.
Operators in C++
Operators are used for performing operations on operands (variables and values). C++ supports many built-in operators. It includes:
i. Arithmetic Operators: +, -, *, /, %
ii. Relational Operators: >, >=, <, <=, ==, !=
iii. Logical Operators: &&, ||, !
iv. Increament & Decreament Operators: ++, --
iv. Assignment Operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, >>=, <<=
v. Bitwise Operators: &, ^, |, ~, >>, <<
vi. Conditional Operators: ?:
vii. Special Operators: sizeof(), new, delete
Examples in C++
Q 1. Write a program to display "Hello World!" on the screen.
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
OUTPUT:
Q 2. Write a program to add two numbers.
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
int a, b, s;
cout<<"Enter any two numbers: ";
cin>>a>>b;
s= a+b;
cout<<"Sum: "<<s;
return 0;
}
OUTPUT:
0 Comments