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

Unit1 2(Operator)

This document serves as an introduction to object-oriented programming with C++, covering fundamental concepts such as syntax, data types, control structures, and the standard C++ library. It provides practical examples and exercises to help learners write and execute basic C++ programs. Additionally, it explains the use of variables, comments, operators, and the differences between C++ and other programming languages like C and Java.

Uploaded by

furryk24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unit1 2(Operator)

This document serves as an introduction to object-oriented programming with C++, covering fundamental concepts such as syntax, data types, control structures, and the standard C++ library. It provides practical examples and exercises to help learners write and execute basic C++ programs. Additionally, it explains the use of variables, comments, operators, and the differences between C++ and other programming languages like C and Java.

Uploaded by

furryk24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DIT1123-Object oriented programming with c++

Chapter 1: Introduction to Object Oriented


Programming and C++ Elements

Kukutla Alekhya
Copyright 2023 ©, ISBAT University, All rights reserved.
Learning Objectives

• Introducing the syntax and structure of C++ programming language, including basic data types,
control structures, functions, classes, and objects.
• Helping learners to understand the differences between C++ and other programming languages
such as C and Java.
• Teaching learners how to write and execute basic C++ programs using an integrated development
environment (IDE) or a compiler.
• Providing an overview of the standard C++ library, including its built-in functions and data
structures.
• Giving learners hands-on practice with C++ programming by guiding them through several examples
and exercises.

2
<iostream>

Header File Function and Description


<iostream> It is used to define the cout,
cin , which correspond to
standard
output stream, standard input
stream and standard error
stream, respectively.
Standard output stream (cout)

• Standard output stream (cout)


The cout is a predefined object of ostream class. It is connected with the standard output device, which
is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display
the output on a console

• Standard input stream (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 a console.

• Standard end line (endl)


The endl is a predefined object of ostream class. It is used to insert a new line characters and flushes the
stream.
#include <iostream>
using namespace std;
int main( )
{
int grade ;
cout << "Enter your grade";
cin >>grade ;
cout << " Your grade: " << grade << endl;
}
Variable

A variable is a name of memory location. It is used to store data. Its value


can be changed and it can be reused many times.
It is a way to represent memory location through symbol so that it can be
easily identified.
the syntax to declare a variable:

datatype variable_list;
int x;
float y;
char z;

int x=5,b=10; //declaring 2 variable of integer type


float f=30.8;
char c='A';
Rules for defining variables

• A variable can have alphabets, digits and underscore.


• A variable name can start with alphabet and underscore
only. It can't start with digit.
• No white space is allowed within variable name.
• A variable name must not be any reserved word or keyword
e.g. char, float etc.
KEYWORDS

Auto break Case char const continue default do

Double else enum extern float for goto if

Int long register return short signed sizeof static

Struct switch typedef union unsigned void volatile while


Comments

• The C++ comments are statements that are not executed by


the compiler.
• The comments in C++ programming can be used to provide
explanation of the code, variable, method or class.
• By the help of comments, you can hide the program code
also.
There are two types of comments in C++.
Single Line comment
Multi Line comment
Single Line Comment

The single line comment starts with // (double slash). Let's see an
example of single line comment in C++.
#include <iostream>
using namespace std;
int main()
{
int x = 11; // x is a variable
cout<<x;
Return 0;
}
Multi Line Comment

The C++ multi line comment is used to comment multiple lines of code. It is
surrounded by slash and asterisk (/∗ ..... ∗/). Let's see an example of multi
line comment in C++.
#include <iostream>
using namespace std;
int main()
{
/* declare and
print variable in C++. */
int x = 35;
cout<<x;
return 0;
}
Operators

An operator is simply a symbol that is used to perform operations.


There can be many types of operations like arithmetic, logical,
bitwise etc.

Precedence of Operators in C++


The precedence of operator species that which operator will be
evaluated first and next. The associativity specifies the operators
direction to be evaluated, it may be left to right or right to left.
Arithmetic operator

#include <iostream>
using namespace std;
int main()
{
int x; // variable declaration.
int y; // variable declaration
int z; // variable declaration
cout<<"Enter the values of x and y";
cin>>x>>y;

z=x+y;
cout<<"Value of z is :"<<z;
return 0;
}
Relational operator

A relational expression is an expression that produces a value of type bool,


which can be either true or false. It is also known as a boolean expression.
#include <iostream>
using namespace std;
int main()
{
int a=45; // variable declaration
int b=78; // variable declaration
bool y= a>b; // relational expression
cout<<"Value of y is :"<<y; // displaying the value of y.
return 0;
}
Value of y is :0
Logical Operator

A logical expression is an expression that combines two or more relational expressions


and produces a bool type value. The logical operators are '&&' and '||' that combines two or more
relational expressions.
#include <iostream>
using namespace std;
int main()
{
int a=2;
int b=7;
int c=4;
cout<<!((a>b)&&(a<c));
return 0;
}
output :
0
Bitwise Operators

A bitwise expression is an expression which is used to manipulate


the data at a bit level. They are basically used to shift the bits.

For example:
x=3
x>>3 // This statement means that we are shifting the three-bit
position to the right.
In the above example, the value of 'x' is 3 and its binary value is
0011. We are shifting the value of 'x' by three-bit position to the
right.
Bitwise Operators
#include <iostream>
using namespace std;
int main()
{
int x=5; // variable declaration
cout << (x>>1) ;
return 0;
}
Output :
2
19

Thank you

© ISBAT UNIVERSITY – 2023 5/11/2023

You might also like