UNIT-1 Lesson 1 C++
UNIT-1 Lesson 1 C++
Unit-1
Topic-1
Introduction to OOP - Language basics: keywords, data types in C++, variable declaration,
reference types,
Input / Output statements: cin and cout using iostream.h Operators and its Precedence ,
Typecasting in C++, Implementing programs using arithmetic operators using cin and cout,
POP stands for Procedural oriented programming is about writing procedures (or) functions
that perform operations on the data, while object-oriented programming is about creating
objects that contain both data and functions.
C++ supports the object-oriented structure programming, the five major pillar of object-oriented
programming (OOPs) used in C++ are:
2. Inheritance
3. Polymorphism
4. Encapsulation
5. Abstraction
#include <iostream>
using namespace std;
main() {
cout << "Welcome to C++ Programming.";
}
#include<iostream> includes the standard input output library functions.
It provides cin and cout built-in objects for reading from console input device keyboard and
writing to console output display in monitor respectively.
The main() function is the entry point of every program in C++ language.
cout << "Welcome to C++ Programming." is used to print the data "Welcome to C++
Programming." on the console output (monitor).
Page 1
Object Oriented Programming with C++ Unit-1
C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of data.
The cin is used in conjunction with stream extraction operator (>>) to read the input from
console input like Keyboard.
C++ uses
1) Uppercase letters A to Z,
2) lowercase letters a to z,
3) Digits 0 to 9, and
4) Special characters (symbols) + - * I - % & # ? { } [ ] ( ) ; “ ” (white space) , %
Page 2
Object Oriented Programming with C++ Unit-1
Tokens in C++
A Token is as the smallest individual unit (element) in creating a C++ program. C++ tokens are
classified by,
o Keywords
o Identifiers
o Strings
o Operators
o Constant
o Special Characters (Punctuators )
C++ Keywords
So, it cannot use it as an identifiers like variable name, function name etc.
(eg) int, float, void, double,this, new, friend, class, inline, private, public, protected, etc.
Identifiers in C++
Names (user defines) are given (represent) for variables, constant, user defines
functions, arrays, structures, unions, class, object, labels in C++ language.
Rules for naming C++ identifiers (or) Rules for naming Variables
o A data type specifies the type of data that a variable can store such as integer, floating,
character etc.
Page 3
Object Oriented Programming with C++ Unit-1
C++ Variable
A variable is a name of memory location (box) with unique reference (address).It is used to
store data. Its value can be changed and it can be reused many times.
Place holder (or) memory box allocation to hold (store) values with unique memory
address.
Variable can be used to value changeable for each program execution (run).
Variable declaration
Declaration of a variable is a statement used to specify the variable name(s) and its
data type.
datatype variable_name;
Example
int a;
int _ab;
int a30;
Page 4
Object Oriented Programming with C++ Unit-1
int 4a;
int x y;
int double;
Reference types in C++
Syntax
Example
Expressions
(eg) c = a+b;
C++ Operators
Page 5
Object Oriented Programming with C++ Unit-1
An operator is simply a symbol that is used to perform specific operation and also
returns specific value.
Here are following Categories (types) of operators to perform different types of operations in
C++ language.
oArithmetic Operators
o Unary Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Conditional Operators
o Assignment Operator
o Comma Operator
Unary operators
Unary operators and expression have only one operand. Operand means as variable or
value.
++ (increment operator)
-- (decrement operator)
! (logical Not operator)
+ (Unary plus)
- (Unary minus)
~ (Complement operator)
Increment and Decrement Operators
Operator Description
Example:
//To Demonstrate prefix and postfix modes.
#include <iostream>
using namespace std;
main()
{
int a=5, b=5, x=10, y=10;
Page 6
Object Oriented Programming with C++ Unit-1
5 4
10 11
Binary operator and expression has two operands. Operand means as Variable or Value.
Arithmetic operators, Relational operators, Logical operators, Assignment & bit wise
operators.
Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
Example:
// C++ Program to Add Two Numbers
#include <iostream>
using namespace std;
main()
{
int i=0xF , j=012;
cout<<“Sum of two numbers is ”<< i+j;
return 0;
}
Output:
Typecasting in C++
Page 7
Object Oriented Programming with C++ Unit-1
Type casting refers to the conversion of one data type to another in a program.
#include <iostream>
using namespace std;
main()
{
float b;
b = 5/2; //2.0
cout<<b;
return 0;
}
Output:
2.00
#include <iostream>
using namespace std;
main()
{
float b;
b = 5/2.0; //automatically type promotion as double
cout<<b;
return 0;
}
Output:
2.5
#include <iostream>
using namespace std;
main() {
int a, b;
Page 8
Object Oriented Programming with C++ Unit-1
float c;
cout<<“Enter two int values: ”);
cin>>a>>b; // 5 2
c = a / (float) b; //Explicitly (Manual) by user with help of (type)
cout<<c; //2.5
return 0;
}
Output:
Enter two int values: 5 2
2.5
Relational Operators
Relational operators are used to comparing two quantities or values and only return 0
(or) 1.
0 means false, 1means true (As Boolean values)
== Is equal to a==b
Example Program
// Relational operators
#include <iostream>
using namespace std;
main()
{
int a =10, b = 5, c;
c = a <b;
cout<<c<<endl;
cout<< b!=a <<endl;
cout<< a>=b;
}
Output
0
Page 9
Object Oriented Programming with C++ Unit-1
1
1
Logical Operators
C++ provides logical operators when test more than one condition to make decisions.
Logical operators return 0 or 1. (As Boolean values) 0 – false 1 - true
Example program
//Mark sheet
#include <iostream>
using namespace std;
main()
{
int m1=50, m2=39,m3=80;
if (m1>=40 && m2>=40 && m3>=40)
cout<<“Result: PASS”;
else
cout<<“Result: FAIL”;
}
Output:
FAIL
Bitwise Operators
C++ provides a special operator for bit operation between only two variables (Binary
operators).
Bit operation means one by one bit manipulation.
Operator Description
Page 10
Object Oriented Programming with C++ Unit-1
Example program
// Binary Bit wise AND , OR , Left Shift operators
#include <iostream>
using namespace std;
main()
{
int a=2, b=3;
cout<<“\nBit wise AND operation: ” << a & b;
cout<<“\nBit wise OR operation: ”<< a | b;
cout<<“\nBit wise Left shift operation: ”<< a << 1;
cout<<“\nBit wise Right shift operation: ”<< a >> 1;
}
Output
Bit wise AND operation: 2
Bit wise OR operation: 3
Bit wise Left shift operation: 4
Bit wise Right shift operation: 1
Assignment Operators
Assignment operators applied to assign or store (Right side to Left side) the result of an
expression (or) value (or) variable to a variable.
(eg) c = a+b;
m = 10;
x = y;
C++ has a collection of Shorthand assignment operators or Compound assignment operators.
= Assign a = 10 a = 10
Page 11
Object Oriented Programming with C++ Unit-1
Example Program
#include <iostream>
using namespace std;
main()
{
int a =10, b;
b = a; // Assignment operator
a+=5; // a = a +5; // a = 15 // Compound assignment operator
b-=5; // b = b – 5; // b = 5 //Shorthand assignment operator
a/=b; // a = a / b; // a = 15/5 // a = 3
cout<<“\n Now a = ”<<a;
}
Output
Now a = 3
Conditional Operator
Operator Description
? True
: False
Example Program
#include <iostream>
using namespace std;
main()
Page 12
Object Oriented Programming with C++ Unit-1
{
int a = 10, b = 20, c;
c = (a > b) ? a : b;
cout<<“Maximum: ”<< c;
}
Output
Maximum: 20
Comma ( ,)operator
The comma operator ( , )is last(lowest) precedence operator for assign multiple values to a
variable using comma.
Example:
a = 10,20,30;
o The precedence of operator species that which operator will be evaluated first and next.
o The associativity specifies the Operators direction to be evaluated; it may be left to
right or right to left.
Page 13
Object Oriented Programming with C++ Unit-1
Page 14