Oop Module 1
Oop Module 1
Oop Module 1
Introduction: Basic concepts of Oops, benefits of oops, application of Oops, Difference between
Procedural Oriented programming and object-oriented programming, c++ Data types, Basic
program construction , , Input output statements: cin and cout, Comments, escape sequence in
C++ ,Operators: types of operators in c++ , library functions,
Structures: A Simple Structure, Defining and accessing the structure, Nested.
What is programming language
A programming language is a set of instructions used to communicate with a computer. It allows
developers to write code that the computer can understand and execute, resulting in a specific
action or output. Popular programming languages include Python, Java, JavaScript, and C++.
OOPs contd.
Procedural Language
C, Fortran etc.
Tough to Handle Lengthy Programs
Divided in Functions/ Modules
Structured Programming
Problems with Structured Programming
Accessibility of Data Across Functions
Global vs. Local Data in Procedural Language
Too many Global Data tough to Handle
Why OOPs contd.
syntax
Cin>> variable name;
example
Cin>> a;
Cout<< a;
Example 1
// C++ program to illustrate the use of cout object
#include <iostream>
using namespace std;
int main()
{
// Print standard output
// on the screen
cout << “My First Program";
return 0;
}
Example 2
// C++ program to illustrate printing of more than one statement in a single cout statement
#include <iostream>
using namespace std;
int main()
{
string name = "Akshay";
int age = 18;
// Print multiple variable on screen using cout
cout << "Name : " << name << endl << "Age : " << age << endl;
return 0;
}
Example 3
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: "; //character output with insertion operator
// take integer input
cin >> num; //character input with extraction operator
cout << "You entered: " << num;
return 0;
}
Symbolic constant
Constant are like variables except that their values cannot be changed
Syntax
Const <data type> < variable name> = <value>;
example
Const float pi =13.4;
C++ Comments
Why Comments are used in C++?
Comments in C++ are used to summarize an algorithm, identify a variable’s purpose, or clarify a
code segment that appears unclear. Comments are also used for:
Comments are used for easier debugging.
It makes a program more readable and gives an overall description of the code.
Comments are helpful in skipping the execution of some parts of the code.
Every time a program or code is reused after long periods of time, the comment recaps all the
information of the code quickly.
Library Functions In C++
Library functions which are also called as “built-in” functions are the functions that are already
available and implemented in C++.
We can directly call these functions in our program as per our requirements. Library functions in
C++ are declared and defined in special files called “Header Files” which we can reference in our
C++ programs using the “include” directive.
<iostream>, <cstring>, <cmath>, <cstdlib> headers that we have used from time to time.
Structure Of C++ Program
C++ Operators
Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while - is an operator used for subtraction.
Operators in C++ can be classified into 6 types:
Arithmetic Operators
Assignment Operators
Relational Operators
Logical Operators
Bitwise Operators
Bitwise Operators in C - Examples and Applications | Hero Vired
Other Operators
Operators in C++
C++ Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For
example,
Unary operators
Increment and Decrement Operators
C++ also provides increment and decrement operators: ++ and -- respectively.
++ increases the value of the operand by 1
-- decreases it by 1
C++ Assignment Operators
Logical operators are used to check whether an expression is true or false. If the expression
is true, it returns 1 whereas if the expression is false, it returns 0.
Explanation of logical operator program
(3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
(3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false).
(3 == 5) && (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
(3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
(3 != 5) || (3 > 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
(3 == 5) || (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
!(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
!(5 == 5) evaluates to 0 because the operand (5 == 5) is 1 (true).
C++ Bitwise Operators
In C++, bitwise operators are used to perform operations on individual bits. They can only be
used alongside char and int data types.
Other C++ Operators
#include <iostream..h>
using namespace std;
int main()
{
int a;
int b;
cout<<"Enter the values of 'a' and ’b'";
cin>>a;
cin>>b;
add=a+b;
sum=a-b;
div=a/b;
mul=a*b;
cout<<“The values that you have entered are : "<<a<<" , "<<b;
return 0;
}
C++ program- Relational
operator
// relational operators
#include <iostream>
using namespace std;
int main()
{int x = 10;
int y = 15;
int z = 10;
cout<<(x < y);
cout<<(y > z);
cout<<(y >= x );
cout<<(z >= x );
cout<<(x==z);
return 0
;}
Escape sequences
◦ Escape sequences are control characters used to move cursor and print some special characters
Structures in C++
Student s;
To access the struct members, we use the instance of the struct and the dot (.)
s. rno = Al23;
Example 1
Example 2
Example 3
Example 4
Brief exaplanation
Sample Structure program
struct Student {
char name[50];
int roll_number;
char grade;
};
int main()
{
struct Student s;
cout << "Enter the rno ";
cin>> s.roll_number;