0% found this document useful (0 votes)
12 views10 pages

document 1

This lesson plan provides an introduction to C++, covering its history, features, and basic syntax. It explains variables, data types, identifiers, operators, and includes examples of input/output operations and calculations. Additionally, it outlines rules for naming variables and identifiers, as well as operator precedence and associativity.
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)
12 views10 pages

document 1

This lesson plan provides an introduction to C++, covering its history, features, and basic syntax. It explains variables, data types, identifiers, operators, and includes examples of input/output operations and calculations. Additionally, it outlines rules for naming variables and identifiers, as well as operator precedence and associativity.
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/ 10

Lesson Plan

Introduction to C++
Introduction to C++
C++ is a middle-level programming language developed by Bjarne Stroustrup in 1979 at Bell Labs
C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. C++ is closer
to the compiler and faster than C language
C++ is both a procedural and an object-oriented programming language. It supports OOP features such as
polymorphism, encapsulation, and inheritance. As C++ is an object-oriented programming language so it
gives a clear structure to programs and allows code to be reused, lowering development costs.

Variables
A variable is the title of a reserved region allocated in memory. In other words, it may be referred

to as the name of a memory location.

Syntax for Declaring a Variable:

<data_type> <variable_name>

Example:
int x = 10; // here int is the data type and x is variable

int y = 50;

Changing values of variables: A variable's value can also be changed in the program.

Example:
int x = 10;

x = 20; // now the value changes to 20

int z = 30;

z = 40; // now the value changes to 40

NOTE:
Assigning value to variable while declaring is optional
Variables Naming Rules
Variables can start from an alphabet or underscore _ or $
Special characters except _ and $ are not allowed
Some particular keywords are not allowed
Commas or blanks are not allowed.

Data Types in C++

Data types specify the different sizes and values that can be stored in the variable.

Hence, by assigning different data types, we can store integers, decimals, or characters in these variables.

Java
C++ &+ DSA
DSA
The different types are :

C++ int: int keyword is used to indicate integers

C++ char: This data type is used to store a single character

C++ bool: A Boolean data type is declared with bool keyword

C++ float : float is used to store floating-point numbers (decimals and exponentials)

C++ double: double is used to store floating-point numbers (decimals and exponentials)

C++ void: The void keyword means "nothing" or "no value".

Identifiers in C++

It is a name given to a package, class, interface, method, or variable. All identifiers must have

different names.

Rules for identifiers in C++ are :

All identifiers should begin with a letter (A to Z or a to z), $ and _ and must be unique

After the first character/letter, identifiers can have any combination of characters

A keyword cannot be used as an identifier

The identifiers are case sensitive

Whitespaces are not permitted.

Examples of Identifiers in C++ are:

abc, _abc, __xyz Output in C++ In C++, all lines that start with a pound or hashtag # sign are called directives

and are processed by a preprocessor which is a program invoked by the compiler. #include<iostream> tells the

preprocessor to include the iostream header file(file that has some already pre-written code which we are

importing to avoid reinventing the wheel) in the program and iostream is the header file which contains all the

basic functions of the program like input/output etc. Using namespace std is used to define which input/output

form is going to be used. (Explained in the forthcoming lectures). int main(): This line is used to declare a

function having the name “main” whose return type is integer. Every C++ program’s execution begins with the

main() function, no matter where the function is located in the program.

Example:

#include <iostream>

using namespace std;

int main()

// prints hello world in C++

cout << "Hello World in C++";

Output: Hello World in C++

Java
C++ &+ DSA
DSA
NOTE :

To print a new line in C++, we use endl command or '\n' command.

Taking Input in C++

This can be done using cin . cin is a predefined object that reads data from the user with the extraction

operator ( >> ) (like scanf is used in case of C language).

Example:

#include <iostream>

using namespace std;

int main() {

int x, y;

cout << "Enter two numbers: ";

cin >> x >> y;

sum = x + y;

// prints sum

cout << sum <<endl;

return 0;

Q1. Take two integers input, x and y : x>y, and find the remainder when x is divided by y.

Solution:

#include <iostream>

using namespace std;

int main() {

int x, y;

cout << "Enter two numbers: ";

cin >> x >> y;

int remainder= x%y;

// prints remainder

cout << remainder<<endl;

return 0;

Q2. Calculating percentage of 5 subjects

#include <iostream>

using namespace std;

int main() {

float sub1,sub2,sub3,sub4,sub5;

cout << "Enter marks of 5 subjects ";

cin >> sub1 >> sub2 >> sub3 >> sub4 >> sub5;

float percent= (sub1+sub2+sub3+sub4+sub5)/5;

// prints percent

cout << percent<<endl;

return 0;

Java
C++ &+ DSA
DSA
Q3. Calculating Area of a Circle

#include <iostream>

using namespace std;

int main() {

int radius;

cin>>radius; 

float area = 2*3.14*radius;

// prints area

cout << area<<endl;

return 0;

C++ Operators

Operators are the symbols that are used to perform pre-defined operations on variables and values
(commonly referred to as operands).

Operators in C++ can be classified into 6 types:


Arithmetic Operator
Relational Operator
Logical Operator
Assignment Operator
Bitwise Operator
Misc. Operator

Arithmetic Operators: They are used in mathematical expressions in a program. They function in

the same way as they do in algebra.

Example:

#include<iostream>

using namespace std;

int main() {

int x = 5;

int y = 10;

int sum = x+y; // using addition operator

int diff = x-y; // using subtraction operator

int div = y/x; // using division operator

int product = x*y;

cout << sum << endl;

cout << diff << endl;

cout << product << endl;

cout << div << endl;

Java
C++ &+ DSA
DSA
C++ Relational Operators:

Operator Description Example

== Is Equal To 4 == 5 returns false

!= Not Equal To 1!= 5 returns true

> Greater Than 1 > 5 returns false

< Less Than 2 < 5 returns true

≥ Greater Than or Equal To 4 >= 5 returns false

<= Less Than or Equal To 1 <= 5 returns true

C++ Logical Operators : Logical operators are used for decision making. This class of operators is used to check
whether an expression is true or false. Some of the commonly used logical operators are mentioned in the table
below.

Operator Example Meaning && (Logical AND) expression1 && expression2 true only if both expression1 and
expression2 are true | (Logical OR) expression1 | expression2 true if either expression1 or expression2 is true !
(Logical NOT) !expression true if expression is false and vice versa

C++ assignment operators:


It assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its

left-hand operand.

Operator Example Equivalent to

= p = q; p = q;

+= p += q; p = p + q;

-= p -= q; p = p - q;

*= p *= q; p = p * q;

/= p /= q; p = p / q;

%= p %= q; p = p % q;

Java
C++ &+ DSA
DSA
Bitwise Operators :
Operator Description
~ Bitwise Complemen
<< Left Shif
>> Right Shif
>>> Unsigned Right Shif
& Bitwise AN
^ Bitwise exclusive OR

Miscellaneous Operators:
1 sizeof
This operator is used to compute the size of a variable. For example, sizeof(p) ,

where ‘p’ is integer, and will return 4.

2 Condition ? Expression1 : Expression2

Conditional operator Condition is true then it returns the value of Expression1 otherwise

it returns the value of Expression2.


3,
Comma operator is a binary operator. The value of the entire comma expression is

actually the value of the last expression of the comma-separated list.

4 . (dot) and -> (arrow)


Member operators are for reference individual members of classes, structures,

and unions.

The dot operator is used for the actual object. The arrow operator is used with a

pointer to an object.

5&
Address-of operator & returns the memory address of a variable. For example &p; will

give the actual memory address of the variable p.

C++ Operator Precedence and Associativity

Operator precedence determines the order/sequence of evaluation of operators in an expression

(analogous to BODMAS concept in math).

Associativity specifies the order in which operators are evaluated by the compiler, which can be

left to right or right to left.

Java
C++ &+ DSA
DSA
Category Operators Associativity

Postfix ++, - - Left to right

Unary +, -, !, ~, ++, - - Right to left

Multiplicative *, /, % Left to right

Additive +, - Left to right

Shift <<, >> Left to right

Relational < <=, > >= Left to right

Equality != , == Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR | Left to right

Conditional ?: Right to left

Assignment += , -=, >> = , << = , ^=, | = Right to left

Q4. Calculate simple interest.


Solution:
#include<iostream>

using namespace std;

int main() {

float p,r,t,si;

p = 100;

r = 10;

t = 2;

si = (p*r*t)/100;

cout<<si;

Java
C++ &+ DSA
DSA
Q5. Which of the following statements is false
(1) Each new C++ instruction has to be written on a separate line 

(2) Usually all C++ statements are entered in small case letters 

(3) Blank spaces may be inserted between two words in a C++ statement 

(4) Blank spaces cannot be inserted within a variable name

Solution:
Fals
Fals
Tru
true

Q6. If a is an integer variable, a = 5 / 2 ; will return a value


(1) 2.5 

(2) 3 

(3) 2 

(4) 0

Solution: Option 3) 2

Q7. What will be the value of d if d is a float after the operation d = 2 / 7.0?

(1) 0 

(2) 0.2857 

(3) Cannot be determined 

(4) None of the above

Solution: option 2 -> 0.2875

Java
C++ &+ DSA
DSA
THANK

YOU !

You might also like