0% found this document useful (0 votes)
11 views

UNIT-1 Lesson 1 C++

Uploaded by

ajaytech198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

UNIT-1 Lesson 1 C++

Uploaded by

ajaytech198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Object Oriented Programming with C++ Unit-1

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,

Object-Oriented Programming (OOPs)

OOP stands for Object-Oriented Programming.

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:

1. Class and Objects

2. Inheritance

3. Polymorphism

4. Encapsulation

5. Abstraction

C++ Programming basics

#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.

The main() function is built-in function directly calling by Compiler.

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++ Input/Output statements

C++ I/O operation is using the stream concept. Stream is the sequence of bytes or flow of data.

Standard output stream

Console output (cout)

 The cout is a predefined object of ostream class.


 It is connected with the standard output device, which is usually a display screen
(Monitor).
 The cout is used in conjunction with stream insertion operator (<<) to display on
console output (monitor).

Standard input stream

Console input (cin)

The cin is a predefined object of istream class.

It is connected with the standard input device, which is usually a keyboard.

The cin is used in conjunction with stream extraction operator (>>) to read the input from
console input like Keyboard.

C++ Character Set

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

as building blocks to form basic program elements.

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

o A keyword is a reserved word.


o A keyword is a fixed pre-defined meaning (action) by compiler.

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.

(eg) a, B, c1, tot, _s5

Rules for naming C++ identifiers (or) Rules for naming Variables

o The first character of an identifier should be either an alphabet (or) an underscore,


o From second character can be followed by alphabet, digit (numbers), or underscore.
o It should not begin with any numerical digit.
o In identifiers, both uppercase and lowercase letters are distinct (Case Sensitive).
o Cannot be used any keyword, white spaces as identifiers.
o Cannot be used any special characters (Symbols) without underscore ( _ ) as
identifiers.

C++ Data Types

o A data type specifies the type of data that a variable can store such as integer, floating,
character etc.

There are 3 categories of data types in C++ language.

Page 3
Object Oriented Programming with C++ Unit-1

Categories Data Types


Basic Data Type (or) int, char, void, float, double
Built-in Data type (or)
Standard Data type
Derived Data Type array, pointer, functions
User Defined Data Type class, structure, union, enum

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).

(eg) a, B, c3, tot, _a5

Variable declaration

 Declaration of a variable is a statement used to specify the variable name(s) and its
data type.

Let's see the syntax to declare a variable:

datatype variable_name;

datatype variable_name1 , variable_name2 , ……….;

Example

The example of declaring variable is given below:


int x;
float y;
char z;
Here, x, y, z are variables and int, float, char are data types.

Valid variable names:

int a;
int _ab;
int a30;

Invalid variable names:

Page 4
Object Oriented Programming with C++ Unit-1

int 4a;
int x y;
int double;
Reference types in C++

 When a variable is declared as a reference, it becomes an alternative (alias) name for an


existing variable.
 A variable can be declared as a reference by putting ‘&’ in the declaration for signifying
the address of a variable or any memory.
 Also, we can define a reference variable as a type of variable that can act as a reference
to another variable.

Syntax

data_type &ref_variable = variable;

Example

//references (or) reference variable


//alias (or) rename between two variables but both have same memory address.
//After references, then both variables are same.
#include<iostream>
using namespace std;
main()
{
int a=10;
int &b = a; // here &b is a reference variable
b=9999;
cout<<a<<" "<<b<<endl;
++a;
cout<<a<<" "<<b<<endl;
cout<<&a<<" "<<&b<<endl; // both have same address with memory box
}
Output
9999 9999
10000 10000
0x23fe34 0x23fe34

Expressions

 An expression is a combination of operators with operands (constants, variables).

(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.

Unary operators are

++ (increment operator)
-- (decrement operator)
! (logical Not operator)
+ (Unary plus)
- (Unary minus)
~ (Complement operator)
Increment and Decrement Operators

Operator Description

++ Increment ( one value will be added)

−− Decrement (one value will be


subtracted)

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

cout<< a--<< --b; //Prefix and Postfix Decrement operators


cout<< x++<<++y; //Prefix and Postfix Increment operators
}
Output:

5 4

10 11

Binary Operators based on operands

 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

Arithmetic Operators are used to performing mathematical calculations.

Operator Description

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus (Remainder part of division for int /


char type only)

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:

Sum of two numbers is 25

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.

Typecasting (Type conversion)

1) Implicitly conversion (Automatic) by compiler:

Priority goes to highest size of type.

2) Explicitly conversion (Manual) by user:

Syntax: (type) variable

Example 1 –Implicit conversion

#include <iostream>
using namespace std;
main()
{
float b;
b = 5/2; //2.0
cout<<b;
return 0;
}
Output:

2.00

Example 2 –Implicit conversion

#include <iostream>
using namespace std;
main()
{
float b;
b = 5/2.0; //automatically type promotion as double
cout<<b;
return 0;
}
Output:

2.5

Example 3 - Explicit type conversion or casting (manual conversion)

#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)

Operator Description Example

== Is equal to a==b

!= Is not equal to x!=y

> Greater than m > 100

< Less than a<b

>= Greater than or equal to J >= 10

<= Less than or equal to i<=n

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

Operator Description Meaning

&& Logical AND It performs logical conjunction of two or more expressions


(condition). (if all expressions evaluate to True, result is True.
If one expression evaluates to False, the result is False)

|| Logical OR It performs a logical disjunction on two or more expressions or


condition. (if atleast one expression evaluate to True, the result
is True)

! Logical NOT It performs logical negation on an expression.

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

<< Binary Bit wise Left Shift Operator

>> Binary Bit wise Right Shift Operator

~ Unary Ones Complement Operator

& Binary Bit wise AND Operator

^ Binary Bit wise XOR Operator

| Binary Bit Wise OR Operator

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.

Operator Description Example Meaning

= Assign a = 10 a = 10

Page 11
Object Oriented Programming with C++ Unit-1

+= Add then assign a+=b a=a+b

-= Subtract then assign x-=y x=x–y

*= Multiply then assign a*=2 a=a*2

/= Divide then assign m/=n m=m/n

%= Modulus then assign n%=2 n=n%2

<<= Left shift and assign a<<=2 a = a << 2

>>= Right shift and assign b>>=1 b = b >> 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

 It is Ternary Operator having 3 operands to decision making for given condition.

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;

Comma ( , ) also separator


While declaration multiple variables and providing multiple arguments in a function, comma
works as a separator.
Example:
int a,b,c;
In this statement, comma is a separator and tells to the compiler that these (a, b, and c) are
three different variables at same type.
Example Program
// Comma ( , ) is lowest precedence in C++
#include <iostream>
using namespace std;
main()
int a;
a = 10, (20,30,40,50);
cout<<a;
}
output
10
Precedence of Operators (or) Order of Evaluation (or) Priority of operators (or)
Hierarchical rule (or) Operator Precedence
in C++ Expression

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.

The precedence and associativity of C++ operators is given below:

Category Operator Associativity


Bracket () [] Left to right
Unary Unary + Unary- ! ~ ++ - - (type)* sizeof Right to left
Multiplicative */% Left to right

Page 13
Object Oriented Programming with C++ Unit-1

Additive +- Left to right


Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Right to left
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Right to left
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

Page 14

You might also like