0% found this document useful (0 votes)
4 views37 pages

C++ Module 2 Notes

C++ notes for 1st year engineering

Uploaded by

saicharanrolex
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)
4 views37 pages

C++ Module 2 Notes

C++ notes for 1st year engineering

Uploaded by

saicharanrolex
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/ 37

C++ Tokens

In C++, tokens can be defined as the smallest building block of C++


programs that the compiler understands. Every word in a C++ source
code can be considered a token.
Types of Tokens in C++
We have several types of tokens each of which serves a specific purpose
in the syntax and semantics of C++. Below are the main types of tokens in
C++:
1. Identifiers
2. Keywords
3. Constants
4. Strings
5. Special Symbols
6. Operators

1. Identifiers
In C++, entities like variables, functions, classes, or structs must be given
unique names within the program so that they can be uniquely identified.
The unique names given to these entities are known as identifiers.
It is recommended to choose valid and relevant names of identifiers to
write readable and maintainable programs. Keywords cannot be used as
an identifier because they are reserved words to do specific tasks. In the
below example, “first_name’ is an identifier.
string first_name = "Raju";

We have to follow a set of rules to define the name of identifiers as


follows:
1. An identifier can only begin with a letter or an underscore(_).
2. An identifier can consist of letters (A-Z or a-z), digits (0-9),
and underscores (_). White spaces and Special characters can
not be used as the name of an identifier.
3. Keywords cannot be used as an identifier because they are
reserved words to do specific tasks. For example, string, int,
class, struct, etc.
4. Identifier must be unique in its namespace.
5. As C++ is a case-sensitive language so identifiers such as
‘first_name’ and ‘First_name’ are different entities.
Here are some examples of valid and invalid identifiers in C++:
Valid Identifiers Invalid Identifiers

_name #name (Cannot start with special character except ‘_’)

Number89 2num (Cannot start with a digit)

first_name first name (Cannot include space)

_last_name_ string (Cannot be same as a keyword)

2. Keywords
Keywords in C++ are the tokens that are the reserved words in
programming languages that have their specific meaning and
functionalities within a program. Keywords cannot be used as an identifier
to name any variables.
For example, a variable or function cannot be named as ‘int’ because it is
reserved for declaring integer data type.
There are 95 keywords reserved in C++. Some of the main Keywords
are:
continu
break try catch char Class const
e

default delete auto else Friend for float

Protecte publi
long new operator private return
d c
continu
break try catch char Class const
e

short sizeof static this typedef enum throw

registe
mutable struct case Switch and or
r

namespac static_cas
goto not Xor bool do
e t

unsigne
double int void Virtual union while
d

3. Constants
Constants are the tokens in C++ that are used to define variables at the
time of initialization and the assigned value cannot be changed after that.
We can define the constants in C++ in two ways that are using the ‘const’
keyword and ‘#define’ preprocessor directive. Let’s see both methods one
by one.
1. Define Constants using the ‘const’ Keyword in C++
Constants are the values that do not change during the execution of the
program once defined. We can make constant any type of data such as
integer, float, string, and characters. A literal is a constant value assigned
to a constant variable.
Syntax
const data_type variable_name = value;

2. Define Constants using the ‘#define’ preprocessor directive in C++


#define preprocessor can be used to define constant identifiers and the
identifier is replaced with the defined value throughout the program where
ever it is used. It is defined globally outside the main function.
Syntax
// The constant_Name is replaced by its value throughout the
program where ever it is used
#define constant_Name value
4. Strings
In C++, a string is not a built-in data type like ‘int’, ‘char’, or ‘float’. It is a
class available in the STL library which provides the functionality to work
with a sequence of characters, that represents a string of text.
When we define any variable using the ‘string’ keyword we are actually
defining an object that represents a sequence of characters. We can
perform various methods on the string provided by the string class such
as length(), push_backk(), and pop_back().
Syntax of declaring a string
string variable_name;

Initialize the string object with string within the double inverted commas
(“).
string variable_name = "This is string";

5. Special Symbols
Special symbols are the token characters having specific meanings within
the syntax of the programming language. These symbols are used in a
variety of functions, including ending the statements, defining control
statements, separating items, and more.
Below are the most common special symbols used in C++
programming:
· Semicolon (;): It is used to terminate the statement.
· Square brackets []: They are used to store array elements.
· Curly Braces {}: They are used to define blocks of code.
· Scope resolution (::): Scope resolution operator is used to
access members of namespaces, classes, etc.
· Dot (.): Dot operator also called member access operator used to
access class and struct members.
· Assignment operator ‘=’: This operator is used to assign values
to variables.
· Double-quote (“): It is used to enclose string literals.
· Single-quote (‘): It is used to enclose character literals.
6. Operators
C++ operators are special symbols that are used to perform operations on
operands such as variables, constants, or expressions. A wide range
of operators is available in C++ to perform a specific type of operations
which includes arithmetic operations, comparison operations, logical
operations, and more.
For example (A+B), in which ‘A’ and ‘B’ are operands, and ‘+’ is an
arithmetic operator which is used to add two operands.
Types of Operators
1. Unary Operators
2. Binary Operators
3. Ternary Operators
1. Unary Operators
Unary operators are used with single operands only. They perform the
operations on a single variable. For example, increment and decrement
operators.
· Increment operator ( ++ ): It is used to increment the value of
an operand by 1.
· Decrement operator ( — ): It is used to decrement the value of
an operand by 1.
2. Binary Operators
They are used with the two operands and they perform the operations
between the two variables. For example (A<B), less than (<) operator
compares A and B, returns true if A is less than B else returns false.
· Arithmetic Operators: These operators perform basic arithmetic
operations on operands. They include ‘+’, ‘-‘, ‘*’, ‘/’, and ‘%’
· Comparison Operators: These operators are used to compare
two operands and they include ‘==’, ‘!=’, ‘<‘, ‘>’, ‘<=’, and ‘>=’.
· Logical Operators: These operators perform logical operations
on boolean values. They include ‘&&’, ‘||’, and ‘!‘.
· Assignment Operators: These operators are used to assign
values to variables and they include ‘variables‘, ‘-=‘, ‘*=‘, ‘/=‘,
and ‘%=’.
· Bitwise Operators: These operators perform bitwise operations
on integers. They include‘&’, ‘|’, ‘^’, ‘~’, ‘<<‘, and ‘>>‘.
3. Ternary Operator
The ternary operator is the only operator that takes three operands. It is
also known as a conditional operator that is used for conditional
expressions.
Syntax:
Expression1 ? Expression2 : Expression3;

If ‘Expression1’ became true then ‘Expression2’ will be executed


otherwise ‘Expression3’ will be executed.

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:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators

1. C++ Arithmetic Operators


Arithmetic operators are used to perform arithmetic operations on variables
and data. For example,

a + b;

Here, the + operator is used to add two variables a and b. Similarly there
are various other arithmetic operators in C++.
Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo Operation (Remainder after division)


Example 1: Arithmetic Operators
#include <iostream>
using namespace std;

int main() {
int a, b;
a = 7;
b = 2;

// printing the sum of a and b


cout << "a + b = " << (a + b) << endl;

// printing the difference of a and b


cout << "a - b = " << (a - b) << endl;

// printing the product of a and b


cout << "a * b = " << (a * b) << endl;

// printing the division of a by b


cout << "a / b = " << (a / b) << endl;

// printing the modulo of a by b


cout << "a % b = " << (a % b) << endl;

return 0;
}
Run Code

Output

a+b=9
a-b=5
a * b = 14
a/b=3
a%b=1

Here, the operators +, - and * compute addition, subtraction, and


multiplication respectively as we might have expected.
/ Division Operator
Note the operation (a / b) in our program. The / operator is the division
operator.
As we can see from the above example, if an integer is divided by another
integer, we will get the quotient. However, if either divisor or dividend is a
floating-point number, we will get the result in decimals.

In C++,

7/2 is 3

7.0 / 2 is 3.5

7 / 2.0 is 3.5

7.0 / 2.0 is 3.5

% Modulo Operator
The modulo operator % computes the remainder. When a = 9 is divided by b
= 4, the remainder is 1.

Note: The % operator can only be used with integers.

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
For example,

int num = 5;

// increment operator
++num; // 6
Here, the code ++num; increases the value of num by 1.

Example 2: Increment and Decrement Operators


// Working of increment and decrement operators

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 100, result_a, result_b;

// incrementing a by 1 and storing the result in result_a


result_a = ++a;
cout << "result_a = " << result_a << endl;

// decrementing b by 1 and storing the result in result_b


result_b = --b;
cout << "result_b = " << result_b << endl;

return 0;
}
Run Code

Output

result_a = 11
result_b = 99

In the above program, we have used the ++ and -- operators as prefixes


(++a and --b). However, we can also use these operators as postfix (a++
and b--).
To learn more, visit increment and decrement operators.
2. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables. For
example,

// assign 5 to a
a = 5;

Here, we have assigned a value of 5 to the variable a.


Operator Example Equivalent to

= a = b; a = b;

+= a += b; a = a + b;

-= a -= b; a = a - b;

*= a *= b; a = a * b;

/= a /= b; a = a / b;

%= a %= b; a = a % b;

Example 3: Assignment Operators


#include <iostream>
using namespace std;

int main() {
int a, b;

// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;

// assigning the sum of a and b to a


a += b; // a = a +b
cout << "a = " << a << endl;

return 0;
}
Run Code

Output

a=2
b=7

After a += b;
a=9

3. C++ Relational Operators


A relational operator is used to check the relationship between two
operands. For example,

// checks if a is greater than b


a > b;

Here, > is a relational operator. It checks if a is greater than b or not.


If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
Operator Meaning Example

== Is Equal To 3 == 5 gives us false


!= Not Equal To 3 != 5 gives us true

> Greater Than 3 > 5 gives us false

< Less Than 3 < 5 gives us true

>= Greater Than or Equal To 3 >= 5 give us false

<= Less Than or Equal To 3 <= 5 gives us true

Example 4: Relational Operators


#include <iostream>
using namespace std;

int main() {
int a, b;
a = 3;
b = 5;
bool result;

result = (a == b); // false


cout << "3 == 5 is " << result << endl;

result = (a != b); // true


cout << "3 != 5 is " << result << endl;

result = a > b; // false


cout << "3 > 5 is " << result << endl;

result = a < b; // true


cout << "3 < 5 is " << result << endl;

result = a >= b; // false


cout << "3 >= 5 is " << result << endl;

result = a <= b; // true


cout << "3 <= 5 is " << result << endl;

return 0;
}
Run Code

Output

3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1

Note: Relational operators are used in decision-making and loops.

4. C++ Logical 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.
Operator Example Meaning

Logical AND.
&& expression1 && expression2
True only if all the operands are true.

Logical OR.
|| expression1 || expression2
True if at least one of the operands is true.

Logical NOT.
! !expression
True only if the operand is false.

In C++, logical operators are commonly used in decision making. To further


understand the logical operators, let's see the following examples,
Suppose,
a=5
b=8

Then,

(a > 3) && (b > 5) evaluates to true


(a > 3) && (b < 5) evaluates to false

(a > 3) || (b > 5) evaluates to true


(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false

!(a < 3) evaluates to true


!(a > 3) evaluates to false

Example 5: Logical Operators


#include <iostream>
using namespace std;

int main() {
bool result;

result = (3 != 5) && (3 < 5); // true


cout << "(3 != 5) && (3 < 5) is " << result << endl;

result = (3 == 5) && (3 < 5); // false


cout << "(3 == 5) && (3 < 5) is " << result << endl;

result = (3 == 5) && (3 > 5); // false


cout << "(3 == 5) && (3 > 5) is " << result << endl;

result = (3 != 5) || (3 < 5); // true


cout << "(3 != 5) || (3 < 5) is " << result << endl;

result = (3 != 5) || (3 > 5); // true


cout << "(3 != 5) || (3 > 5) is " << result << endl;

result = (3 == 5) || (3 > 5); // false


cout << "(3 == 5) || (3 > 5) is " << result << endl;

result = !(5 == 2); // true


cout << "!(5 == 2) is " << result << endl;

result = !(5 == 5); // false


cout << "!(5 == 5) is " << result << endl;

return 0;
}
Run Code

Output

(3 != 5) && (3 < 5) is 1
(3 == 5) && (3 < 5) is 0
(3 == 5) && (3 > 5) is 0
(3 != 5) || (3 < 5) is 1
(3 != 5) || (3 > 5) is 1
(3 == 5) || (3 > 5) is 0
!(5 == 2) is 1
!(5 == 5) is 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).
5. 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.
Operator Description

& Binary AND

| Binary OR

^ Binary XOR

~ Binary One's Complement

<< Binary Shift Left

>> Binary Shift Right

1. C++ Bitwise AND Operator


The bitwise AND & operator returns 1 if and only if both the operands
are 1. Otherwise, it returns 0.
The following table demonstrates the working of the bitwise AND operator.
Let a and b be two operands that can only take binary values i.e. 1 and 0.
a B a&b

0 0 0

0 1 0

1 0 0
1 1 1

Note: The table above is known as the "Truth Table" for the bitwise
AND operator.

Let's take a look at the bitwise AND operation of two integers 12 and 25:

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

//Bitwise AND Operation of 12 and 25

00001100

& 00011001

_________

00001000 = 8 (In decimal)

Example 1: Bitwise AND


#include <iostream>
using namespace std;

int main() {
// declare variables
int a = 12, b = 25;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
return 0;
}
Run Code

Output

a = 12
b = 25
a&b=8

In the above example, we have declared two variables a and b. Here, notice
the line,

cout << "a & b = " << (a & b) << endl;

Here, we are performing bitwise AND between variables a and b.

2. C++ Bitwise OR Operator


The bitwise OR | operator returns 1 if at least one of the operands is 1.
Otherwise, it returns 0.
The following truth table demonstrates the working of the bitwise
OR operator. Let a and b be two operands that can only take binary values
i.e. 1 or 0.
a B a|b

0 0 0

0 1 1

1 0 1

1 1 1
Let us look at the bitwise OR operation of two integers 12 and 25:

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

Bitwise OR Operation of 12 and 25

00001100

| 00011001

_________

00011101 = 29 (In decimal)

Example 2: Bitwise OR
#include <iostream>

int main() {
int a = 12, b = 25;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "a | b = " << (a | b) << endl;

return 0;
}
Run Code

Output

a = 12
b = 25
a | b = 29

The bitwise OR of a = 12 and b = 25 gives 29.


3. C++ Bitwise XOR Operator
The bitwise XOR ^ operator returns 1 if and only if one of the operands
is 1. However, if both the operands are 0, or if both are 1, then the result
is 0.
The following truth table demonstrates the working of the bitwise
XOR operator. Let a and b be two operands that can only take binary
values i.e. 1 or 0.
a b a^b

0 0 0

0 1 1

1 0 1

1 1 0

Let us look at the bitwise XOR operation of two integers 12 and 25:

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25

00001100

^ 00011001

_________

00010101 = 21 (In decimal)


Example 3: Bitwise XOR
#include <iostream>

int main() {
int a = 12, b = 25;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "a ^ b = " << (a ^ b) << endl;

return 0;
}
Run Code

Output

a = 12
b = 25
a ^ b = 21

The bitwise XOR of a = 12 and b = 25 gives 21.

4. C++ Bitwise Complement Operator


The bitwise complement operator is a unary operator (works on only one
operand). It is denoted by ~ that changes binary digits 1 to 0 and 0 to 1.

Bitwise Complement
It is important to note that the bitwise complement of any integer N is
equal to -(N + 1). For example,
Consider an integer 35. As per the rule, the bitwise complement
of 35 should be -(35 + 1) = -36. Now, let's see if we get the correct answer
or not.

35 = 00100011 (In Binary)

// Using bitwise complement operator

~ 00100011

__________

11011100

In the above example, we get that the bitwise complement


of 00100011 (35) is 11011100. Here, if we convert the result into decimal
we get 220.
However, it is important to note that we cannot directly convert the result
into decimal and get the desired output. This is because the binary
result 11011100 is also equivalent to -36.

C++ Shift Operators


There are two shift operators in C++ programming:

· Right shift operator >>


· Left shift operator <<
5. C++ Right Shift Operator
The right shift operator shifts all bits towards the right by a certain
number of specified bits. It is denoted by >>.
When we shift any number to the right, the least significant bits are
discarded, while the most significant bits are replaced by zeroes.

One bit Right Shift

As we can see from the image above, we have a 4-bit number. When we
perform a one-bit right shift operation on it, each individual bit is shifted to
the right by 1 bit.
As a result, the right-most bit is discarded, while the left-most bit remains
vacant. This vacancy is replaced by a 0.

6. C++ Left Shift Operator


The left shift operator shifts all bits towards the left by a certain number
of specified bits. It is denoted by <<.
One bit Left Shift

As we can see from the image above, we have a 4-bit number. When we
perform a 1 bit left shift operation on it, each individual bit is shifted to the
left by 1 bit.
As a result, the left-most bit is discarded, while the right-most bit remains
vacant. This vacancy is replaced by a 0.

Example 5: Shift Operators


#include <iostream>

int main() {

// declaring two integer variables


int num = 212, i;

// Shift Right Operation


cout << "Shift Right:" << endl;

// Using for loop for shifting num right from 0 bit to 3 bits
for (i = 0; i < 4; i++) {
cout << "212 >> " << i << " = " << (212 >> i) << endl;
}

// Shift Left Operation


cout << "\nShift Left:" << endl;
// Using for loop for shifting num left from 0 bit to 3 bits
for (i = 0; i < 4; i++) {
cout << "212 << " << i << " = " << (212 << i) << endl;
}

return 0;
}
Run Code

Output

Shift Right:
212 >> 0 = 212
212 >> 1 = 106
212 >> 2 = 53
212 >> 3 = 26

Shift Left:
212 << 0 = 212
212 << 1 = 424
212 << 2 = 848
212 << 3 = 1696

6. Other C++ Operators


Here's a list of some other common operators available in C++. We will
learn about them in later tutorials.

Operator Description Example

sizeof returns the size of data type sizeof(int); // 4

string result = (5 > 0) ? "even" : "odd


?: returns value based on the condition
"even"
& represents memory address of the operand &num; // address of num

accesses members of struct variables or class


. s1.marks = 92;
objects

used with pointers to access the class or struct


-> ptr->marks = 92;
variables

<< prints the output value cout << 5;

>> gets the input value cin >> num;

Scope Resolution Operator in C++


This section will discuss the scope resolution operator and its various uses in the C++
programming language. The scope resolution operator is used to reference the global
variable or member function that is out of scope. Therefore, we use the scope
resolution operator to access the hidden variable or function of a program. The
operator is represented as the double colon (::) symbol.

For example, when the global and local variable or function has the same name in a
program, and when we call the variable, by default it only accesses the inner or local
variable without calling the global variable. In this way, it hides the global variable or
function. To overcome this situation, we use the scope resolution operator to fetch a
program's hidden variable or function.

Uses of the scope resolution Operator


1. It is used to access the hidden variables or member functions of a program.
2. It defines the member function outside of the class using the scope resolution.
3. It is used to access the static variable and static function of a class.
4. The scope resolution operator is used to override function in the Inheritance.

Program to access the hidden value using the scope


resolution (::) operator
Program1.cpp
1. #include <iostream>
2. using namespace std;
3. // declare global variable
4. int num = 50;
5. int main ()
6. {
7. // declare local variable
8. int num = 100;
9.
10. // print the value of the variables
11. cout << " The value of the local variable num: " << num;
12.
13. // use scope resolution operator (::) to access the global variable
14. cout << "\n The value of the global variable num: " << ::num;
15. return 0;
16. }

Output

The value of the local variable num: 100


The value of the global variable num: 50

Program to define the member function outside of the class


using the scope resolution (::) operator
Program2.cpp

1. #include <iostream>
2. using namespace std;
3. class Operate
4. {
5. public:
6. // declaration of the member function
7. void fun();
8. };
9. // define the member function outside the class.
10. void Operate::fun() /* return_type Class_Name::function_name */
11. {
12. cout << " It is the member function of the class. ";
13. }
14. int main ()
15. {
16. // create an object of the class Operate
17. Operate op;
18. op.fun();
19. return 0;
20. }

Output

It is the member function of the class.

Program to demonstrate the standard namespace using the


scope resolution (::) operator
Program3.cpp

1. #include <iostream>
2. int main ()
3. {
4. int num = 0;
5.
6. // use scope resolution operator with std namespace
7. std :: cout << " Enter the value of num: ";
8. std::cin >> num;
9. std:: cout << " The value of num is: " << num;
10. }

Output

Enter the value of num: 50


The value of num is: 50

Program to access the static variable using the scope


resolution (::) operator
Program4.cpp

1. #include <iostream>
2. using namespace std;
3. class Parent
4. {
5. static int n1;
6. public:
7. static int n2;
8. // The class member can be accessed using the scope resolution operator.
9. void fun1 ( int n1)
10. {
11. // n1 is accessed by the scope resolution operator (:: )
12. cout << " The value of the static integer n1: " << Parent::n1;
13. cout << " \n The value of the local variable n1: " << n1;
14. }
15. };
16. // define a static member explicitly using :: operator
17. int Parent::n1 = 5; // declare the value of the variable n1
18. int Parent::n2 = 10;
19. int main ()
20. {
21. Parent b;
22. int n1 = 15;
23. b.fun1 (n1);
24. cout << " \n The value of the Base::n2 = " << Parent::n2;
25. return 0;
26. }

Output

The value of the static integer n1: 5


The value of the local variable n1: 15
The value of the Base::n2 = 10

Program to access the static member function using the


scope resolution (::) operator
Program5.cpp

1. #include <iostream>
2. using namespace std;
3. class ABC
4. {
5. public:
6. // declare static member function
7. static int fun()
8. {
9. cout << " \n Use scope resolution operator to access the static member. ";
10. }
11. };
12. int main ()
13. {
14. // class_name :: function name
15. ABC :: fun ();
16. return 0;
17. }

Output

Use scope resolution operator to access the static member.

Program to override the member function using the scope


resolution (::) operator
Program5.cpp

1. #include <iostream>
2. using namespace std;
3. class ABC
4. {
5. // declare access specifier
6. public:
7. void test ()
8. {
9. cout << " \n It is the test() function of the ABC class. ";
10. }
11. };
12. // derive the functionality or member function of the base class
13. class child : public ABC
14. {
15. public:
16. void test()
17. {
18. ABC::test();
19. cout << " \n It is the test() function of the child class. ";
20. }
21. };
22. int main ()
23. {
24. // create object of the derived class
25. child ch;
26. ch.test();
27. return 0;
28. }

Output

It is the test() function of the ABC class.


It is the test() function of the child class.

What is Expression in C++ Programming?


C++ Expression is a particular entity that contains constants, operators, and
variables and arranges them according to the rules of the C++ language. If the
question is what is an expression in C++ then it can be answered that the c++
expression contains function calls that return values. Every expression
generates some values that are assigned to that particular variable with the
help of the assignment operator.

Example of what is an expression in C++:


1. (a-b) + c
2. (x*y) -z
3. 4a2 - 5b +c
4. (a+b) / (x+y)
Read More - C++ Interview Interview Questions and Answers

Different types of expressions in C++


There are different types of expression in C++ programming language is
generated, which are:
· Constant expressions
· Integral expressions
· Float expressions
· Pointer expressions
· Relational expressions
· Logical expressions
· Bitwise expressions
· Special assignment expressions

1.) Constant Expression in C++


What is constant in C++ programming language can be explained as this is an
expression that contains only constant values. The value of this expression is
determined in compile time but it is evaluated in run time. Constant definition in
C++ can be composed of integer, floating point, character, and enumeration
constants.
· A constant can be used in the following situations,
· It can be used in the declarator of the subscript for describing
the array bound.
· It can be used after the keyword, case in the switch statement.
· It can be used as a numeric value in an “enum”
· It helps to specify a bit-field width.
· It is used as the pre-processor in the if statement
Expression containing constant Constant value

x = (2/5) * 4 (2/5) * 4

extern int y = 89 89

int z = 21 21

static int a = 60 60

Example in C++ Editor


#include <iostream>
using namespace std;

int main() {
int a; // variable declaration.
a = (4 / 3) + 2; // constant expression
cout << "The value of a is: " << a; // displaying the value of a.
return 0;
}
Run Code >>

Output
The value of a is: 3

2.) Integral Expression in C++


An integral expression is a type of expression in C++ that helps to produce the
integer as output after doing all the implicit and explicit conversions.

Example of integral expression in C++


#include <iostream>
using namespace std;

int main() {
int a; // variable declaration.
int b; // variable declaration
int c; // variable declaration
a=8;
b=9;
c = a + b;
cout << "\nValue of c is: " << c; // displaying the value of c.
return 0;
}
Run Code >>

Output
Value of c is:17

3.) Float Expression in C++


Float expressions produce floating point values as an output of a program after
doing all the explicit and implicit conversions.

Example of float expression in C++

#include <iostream>
using namespace std;
int main() {
float a = 8.9; // variable initialization
float b = 5.6; // variable initialization
float c; // variable declaration

c = a + b;
cout << "The value of c is: " << c << endl; // displaying the value of c.
return 0;
}
Run Code >>

Output
The value of c is:14.5

4.) Pointer Expression in C++


This particular pointer expression in C++ produces an address value as an
output of the program.

Example of pointer expression in C++

#include <iostream>
using namespace std;

int main() {
int X[] = {1, 2, 3, 4, 5}; // array initialization
int *ptr; // pointer declaration
ptr = X; // assigning the base address of the array to the pointer ptr
ptr = ptr + 1; // incrementing the value of the pointer

cout << "Value of the second element of an array: " << *ptr << endl;
return 0;
}
Run Code >>

Output
value of the second element of an array: 2

5.) Relational Expression in C++


This Relational expression produces a particular type of value that can be either
true or false which is also known as a boolean expression. If any arithmetic
expressions are used on both sides of the Relational operator then the
arithmetical expression will evaluate first then it will compare the results.
Example of relational expression in C++

#include <iostream>
using namespace std;

int main() {
int X = 45; // variable declaration
int Y = 78; // variable declaration
bool Z = X > Y; // relational expression
cout << "The value of Z is: " << Z << endl; // displaying the value of Z.
return 0;
}
Run Code >>

Output
The value of Z is : 0

6.) Logical Expression in C++


Logical expression in C++ combines two or more relational expressions to
produce a Boolean type of value.

Example of logical expression in C++ Online


Compiler
#include <iostream>
using namespace std;

int main()
{
int x = 2;
int y = 7;
int z = 4;

cout << ((x > y) || (x > z));

return 0;
}

Run Code >>

Output
0
7.) Bitwise Expression in C++
Bitwise expressions are used to manipulate data at a bit level for shifting it to
bits.

Example of bitwise expression in C++

#include <iostream>
using namespace std;

int main()
{
int A = 5; // variable declaration
cout << (A >> 1) << endl;
return 0;
}
Run Code >>

Output
2

8.) Special assignment Expression in C++


Special assignments in C++ can be classified depending on the value of the
variable that is assigned
· Chained assignment expressions- in chained assignment expression
assigns the same values more than once by using only one statement

Example-1

#include <iostream>
using namespace std;

int main()
{
int x; // variable declaration
int y; // variable declaration
x = y = 80; // chained assignment
cout << "Values of 'x' and 'y' are: " << x << "," << y << endl;
return 0;
}
Run Code >>

Output
Values of 'x' and 'y' are : 80,80
· Embedded assignment expressions- In this embedded assignment
expression one expression is enclosed within another assignment
expressions

Example-2

#include <iostream>
using namespace std;

int main()
{
int x; // variable declaration
int y; // variable declaration
x = 10 + (y = 90); // embedded assignment expression
cout << "Value of 'x' is " << x << endl;
return 0;
}
Run Code >>

Output
Values of 'x' is 100

9.) Compound assignment expressions-


This particular assignment operator combines the assignment operator and
binary operator.
Example of compound assignment expressions
in C++ Online Compiler
#include <iostream>
#include
using namespace std;

int main()
{
int x = 10; // variable declaration
x += 10; // compound assignment
cout << "The value of x is: " << x << endl; // displaying the value of x.
return 0;
}
Run Code >>

Output
The value of x is: 20

You might also like