0% found this document useful (0 votes)
71 views22 pages

TOPIC 3 - Language Constructs of C++

Uploaded by

Peter Wamwea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
71 views22 pages

TOPIC 3 - Language Constructs of C++

Uploaded by

Peter Wamwea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 22

PROGRAM: DCS/DSE/DIT/DBIT

UNIT: Object Oriented Programming


SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
INTRODUCTION TO C++ LANGUAGE
C+ + is a superset of C i.e. it was derived from C. Contrary to C, C++ is an object-oriented
programming language. It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in
Murray Hill, New Jersey, USA, in the early 1980’s.

The most important facilities that C++ adds on to C are


(i) Classes;
(ii) Inheritance;
(iii) function overloading; and
(iv) Operator overloading.

N.B: The above features enable creating of abstract data types, inherit properties from
existing data types and support polymorphism, thereby making C++ a truly object-oriented
language.

APPLICATION OF C++
C++ can be used to develop
(i) editors;
(ii) compilers;
(iii) databases;
(iv) communication systems; and
(v) Any complex real life application systems.

STRUCTURE OF C++ PROGRAM


C++ program would contain four sections as shown in fig. 1.1.
It is a common practice to organize a program into three sections:
(i) Declare header files
(ii) Declare the class
(iii) Define of member functions
(iv) Declare the Main function

N.B: The class declarations are placed in another section after the header files.
The definitions of member functions go into another section after the class declaration.
Also, the main function program that uses the class is placed in another section which
includes block of statements.

Trainer: Simon Wamwea.


Contact 0717871829 1
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP

Header Files
Class declaration

Member functions definitions

Main function

Block of statements
Table 1: Structure of a C++ program

LAYOUT OF A SIMPLE C++ PROGRAM


#include<iostream.h>
int main( )
{
Variable declarations
Statement1;
Statement2;
Statement last;
return 0;
}

TYPICAL STEPS/STAGES OF PROGRAMMING IN OOP


(i) Creating the Program.
The programmer defines classes that describe objects that the program will
use when it is running.
The programmer defines a class that contains the main(c) method which is
used to start the program running.
(ii) Compiling the Program.
The program is compiled into byte-code.
(iii) Running the Program.
The compiler looks for a static main( ) method and starts running it.

Trainer: Simon Wamwea.


Contact 0717871829 2
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
As the program runs, objects are created and their methods are activated.
The program does its work by creating objects and activating their methods.
The exact order of object creation and method activation depends on to task to
be performed and the input data.

Displaying_Text Program Example:


Write a C++ program that prints text on the screen.

#include<iostream.h>
Using namespace std;
int main( )
{
cout<<” My name is Irene and I am a C++ programmer”<<endl;
return 0;
}

Explanation:
#<include<iostream>:
The include tells the compiler where to find information about certain items that are used in your
program.

In this case iostream.h is the name of a library that contains the definitions of the routines that
handle input from the keyboard and output to the screen.
iostream is a file that contains some basic information about this library directive in the
program.

The include directive instructs the compiler to include the contents of the file enclosed
within angular brackets into the source file.
The header file iostream.h should be included at the beginning of all programs that use
input/output statements.
Namespace:
Namespace refer to the memory space allocated for names used in a program.
Namespaces are used to organize code into logical groups and to prevent name collisions that
can occur, especially when your code base includes multiple libraries.

Trainer: Simon Wamwea.


Contact 0717871829 3
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
return 0: the line return 0, says “End the program when you get here”. This line need not be the
last thing in the program, but in every simple program, it makes no sense to place the curly brace
anywhere else.

Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI
C++ programs must include this directive. This will bring all the identifiers defined in std to
the current global scope. Using and namespace are keywords/reserved words in C++.

Return Type of main( ): main ( ) returns an integer value to the operating system.
Therefore, every main ( ) in C++ should end with a return 0 statement; otherwise a
warning an error might occur. Since main ( ) returns an integer type for main ( ) is
explicitly specified as int. Note that the default return type for all function in C++ is int.

N.B: The following main without type and return will run with a warning:

main ( )
{
/* Your Block of Statements*/
}

The above example contain only one function main( ). As usual execution begins at main( ).
Every C++ program must have a main( ). The C++ statements terminate with semicolons. To
insert the cursor in the initial position of the next line, we use <<endl; or <<“\n”;

CONSTRUCTS OF C++ LANGUAGE

I. COMMENTS
C++ introduces a new comment symbol // (double slash).
Comments start with a double slash symbol and terminate at the end of the line.
A comment may start anywhere in the line, and whatever follows till the end of the line is
ignored.

N.B: There is no closing symbol for // comment symbol, but there is closing symbol for
the /* comment symbol which is */

The double slash comment using the // symbol, is basically a single line comment:
// This is an example of a single line comment in C++

Trainer: Simon Wamwea.


Contact 0717871829 4
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
Multiline comments using the // symbol, can be written as follows:
// This is an example
// to illustrate
// How multiline comments are used in C++

C++ also uses symbols /*,*/ for comments and are still valid and are more suitable for
multiline comments. The following comment is allowed:

/* This is an example of C++ program to illustrate some of its features */

II. KEYWORDS/RESERVED WORDS


These are words that have a predefined meaning in C++.
C++ keywords/reserved words:

asm double new switch


auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete int static volatile
do long struct while

III. INPUT AND OUTPUT IN C++

INPUT IN C++:
This statement introduces two new C++ features, cin and >>
The identifier cin (pronounced as C in) is a predefined object that represents the standard
input stream in C++.
Here, the standard input stream represents the keyboard.
The operator >> is called the ejection or get from operator.

Syntax: cin>> identifier_name; `

Trainer: Simon Wamwea.


Contact 0717871829 5
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
Example1: cin>>width;

Example2: cin>>length;

Explanation: Causes width and length to be captured from the keyboard.


N.B: We can have a cascading cin>> in C++ which causes two or more inputs to be
ejected from the keyboard using one line of code as follows:

cin>>width>>length;

OUTPUT IN C++:
This statement introduces two new C++ features, cout and <<
The identifier cout (pronounced as C out) is a predefined object that represents the
standard output stream in C++.
Here, the standard output stream represents the screen.
It is also possible to redirect the output to other output devices.
The operator << is called the insertion or put to operator.

Syntax: cout<<

Example1: cout<<”I am a C++ Programmer”;

Explanation1: Causes the string in quotation marks to be displayed on the screen.

Example2: cout<<Area<<”\n”;

Explanation2: Causes the Area (i.e. length and width) to be computed and displayed on

the screen.

N.B: We can use the cout the insertion operator << repeatedly in the last two statements for
printing results.

cout << sum <<Average<<endl;

IV. STATEMENTS
A statement is a line of code (delimited with a semicolon at the end), that performs a
specific task after being compiled and executed (ran).
N.B: Two or more statements are referred to as block of statements.

Trainer: Simon Wamwea.


Contact 0717871829 6
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
Calculate_Average Example: Write C++ program that asks a user to enter two numbers. The
Program computes the average and displays the answer on the
screen.
Solution:

#include<iostream.h> // include header file

Using namespace std;

int main()

Float number1, number2,sum, average; // Declare variables

cin >> number1; // Read Numbers

Cin >> number2; // from keyboard

Sum = number1 + number2;

Average = sum/2;

cout << ”Sum = “ << sum << “\n”;

cout << “Average = “ << average << “\n”;

Return 0;

} //end of example

The output would be:

Enter two numbers: 6.5 7.5


Sum = 14
Average = 7

Trainer: Simon Wamwea.


Contact 0717871829 7
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
V. VARIABLES
Is a named memory location that contains a value that can change during program
execution. C++ variables can hold numbers or other types of data. The number or other
data held in a variable is called its value.

Every variable in a C++ program must be declared. When you declare a variable, you are
telling the compiler and ultimately , the computer what kind of data you’ll be storing in
the variable.

The kind of data that is held in a variable is called its type and the name for the type such
as int or double is called the type name.

Some Type Names include:

Type Name Memory Size Range Precision


Use
char 1 byte -128 to 127 or 0 to 255. Not applicable

int 2 bytes -32,767 to 32,767 Not applicable


long or long int 4 bytes -2,147,403647 to 2,147,403,647 Not applicable
float 4 bytes Approximately 10 -38 to 1038 7 digits
double 8 bytes Approximately 10-308 to 10308 15 digits
long double 10 bytes Approximately 10-4932 to 104932 19 digits
Bool true or false Not applicable
Table 2: Type Names

Validating Variable Names


Variable Name Reason Validity
7th First character is a digit. Not a valid variable name
Void Void is a reserved word Not a valid variable name
Time? ? is a special character Not a valid variable name
My Salary It has a space (which is a special character) Not a valid variable name
Radius Starts with a letter and not a reserved word Valid
Gross_Pay Starts with a letter and not a reserved word Valid

Trainer: Simon Wamwea.


Contact 0717871829 8
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
_root Starts with a letter and not a reserved word Valid
Student1 Starts with a letter and not a reserved word Valid
Table 3: Valid and Invalid Variable Names

Naming of Variables
When naming variables, there are some rules that should be followed. These are:
i. A variable can be made up of letter, digits and underscore ( _ ).
ii. No other special character should be present or allowed.
iii. First character should be a letter or an underscore ( _ ). It can be followed by letters,
digits or underscore.
iv. Upper case and lower case letters are significant. Both cases are allowed. MARKS,
Marks and marks are considered to be three different variables.
v. Reserved words cannot be used as variable names.

Variable Declarations

The syntax for declaring variable is:

Type_Name Variable_List;

Examples:
int a; //a is declared as integer.
int x,y,z; //x,y and z are declared as integers
float basic_salary; //basic_salary is declared as floating point variable
double house_allowance ; //House_allowamce has been declared as type double;
char Grade; //Grade has been declared as of type char.

Variable Initializations

The syntax for declaring variable is:

Type_Name Variable = value;

Examples:
int a = 10; //a is declared as integer and initialized with 10.
int x= 10, y=20, z=30; /* x,y and z are declared as integers and

Trainer: Simon Wamwea.


Contact 0717871829 9
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
initialized with 10, 20 and 30 respectively*/
float basic_salary = 200000; /*basic_salary is declared as floating point
variable and initialized with 200000*/
double house_allowance = 50000; /*House_allowamce has nbeen declared as type
double and initialized with 50000*/
char Grade = „ B‟; /*Grade has been declare as of type char and
initialized with 50000*/

char County = “Nairobi”; /* County has been declare as of type char and
initialized with Nairobi*/

VI. CONSTANTS

A constant is a symbolic name that holds a value that cannot change during program
execution (running) or when the program stops.

Constant Declaration Syntax:

const Type_Name Constant_Name;

Example:

const double pi;

Constant Initialization Syntax:

const double pi;

pi = 3.14;

More Examples:

const int a; //a is declared as integer constant

const int x,y,z; //x,y and z are declared as integer constants

const float basic_salary; //basic_salary is declared as floating point constant

const double house_allowance ; //House_allowamce has nbeen declared as type


double constant

Trainer: Simon Wamwea.


Contact 0717871829 10
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
const char Grade; //Grade has been declare as constant of type

char

const char County; //county has been declare as constant of type char

Constant Initializations

The syntax for initializing a constant is:

const Type_Name constant_name = value;

Examples:
const int a = 10; //a is declared as integer constant and initialized with
10
const int x= 10, y=20, z=30; /* x,y and z are declared as integer constants and
initialized with 10, 20 and 30 respectively*/
const float basic_salary = 200000; /*basic_salary is declared as floating point
constant and initialized with 200000*/
const double house_allowance = 50000; /*House_allowamce has nbeen declared
as type double constant and initialized
with 50000*/
const char Grade = „ B‟; /*Grade has been declare as of type
char constant and initialized with
50000*/

const char Grade = „ B‟; /*Grade has been declare as of type


char constant and initialized with
50000*/
const char County = “Nairobi”; /* County has been declare as of type
char constant and initialized with
Nairobi*/

Trainer: Simon Wamwea.


Contact 0717871829 11
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
VII. EXPRESSIONS
A C++ expression is a combination of operators, constants, variables and function calls that
result in a single value. An expression is always a part of a statement.
Expressions are generally classifies as arithmetic, relational, and logical expressions.

VIII. STATEMENTS
Statements are language constructs that represent a set of declarations or steps in a
sequence of actions.
Types of statements:
(i) Null statement: a null statement is represented by a single semicolon which is the
statement delimiter.
(ii) Expression statement: an expression statement is a combination of operators,
constants, variables and function calls that result in as
single value.
Examples of expression statements:
 c = a + b;
 Age>= 18;
 Answer = sqrt(number);
 Power = pow(number,2);
 Marks>= 70 && Marks<=100 etc.

(iii) Block of statement: some statements can be grouped together to make a block by
giving them within curry braces ({}). Such blocking is
made for a specific purpose such as for executing a block
of statements repeatedly. A block of statements may give
its owner a set of declarations and initializations.
Example of block of statements:

{
house_allowance = 0.01 * basic_salary;
transport_allowance=0.12 * basic_salsrh;

Trainer: Simon Wamwea.


Contact 0717871829 12
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
gross_salry= basic_salsry+house_allowance+transport_allowance;
tax = 0.12 *gross_salary;
net_salary = gross_salary – tax;
}

(iv) Labeled statements: any label may be preceded by a prefix that declares an
identifier as a label name. labels in themselves do not alter
the flow of control but give provision so that other statements
can transfer control to the label statements.
(v) Conditional control statements: the statements through which the flow is
controlled are known as control statements.
Examples of conditional control statements:
 if
 if..else
 Nested if..else
(vi) Loop control statements: the repeated execution of the same block of statements is
possible with loop statements.
Example of loop control statements:
 while
 do..while
 for

IX. OPERATORS AND EXPRESSIONS


An operator is an expression to be performed that yields a value. An operand is an entity on
which an operator acts.
Operators are classified as:
(i) Arithmetic operators
(ii) Relational operators
(iii) Logical operators
(iv) Assignment operator

Trainer: Simon Wamwea.


Contact 0717871829 13
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
(i) Arithmetic Operators
* Asterisk for multiplication
/ Slash for division
% Modulus operator for remainder (cannot be applied to float or double)
+ Plus
- minus

Hierarchy of Arithmetic Operators


The C++ compiler follows a hierarchy to execute the sequence of operators and numbers in
order.
N.B: Unary operator: has one operand. E.g. + + b. here + + is the operator, while b is the
Operand.
Example:
a++;
a--;
++a;
- -a;
Binary operator: Has two operands.
Examples:
c = a + b;
c = a - b;
c = a * b;
c = a / b;
Ternary operator:
(a > b) ? a : b note: if a > b, it returns a, if not it returns b

The hierarchy of arithmetic operators is given below:


(i) * / % Evaluate from left to right, then
(ii) + - Evaluate from left to right
N.B: The expressions within parenthesis are evaluated first following the above hierarchy.
The expression in the inner most parentheses are evaluated first.

Trainer: Simon Wamwea.


Contact 0717871829 14
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
Examples:
(i) x + y – z ; x and y are added first and then z is subtracted from the net result.
(ii) a + b / c; b is divided by c and then the net result is added with. a
(iii) a + b * c / d % e ((8 * c) + 30/ (b * d); All students to evaluate (ii) in
their books for marking.

Increment / Decrement Operators


In some environments, situations may arise to increase or decrease a value by 1 continuously for
some time. C++ provides special characters + + and - - to do this.
This increment operation can be of two types:
(i) first increment or decrement, then take the value for processing (prefix).
(ii) Take the value for processing and then increment or decrement the variable (postfix).

(i) Prefix Increment / Decrement


Examples:
a) K = 2;
L = + + K;
Explanation:
K is assigned with the value 2. Firstly, because it’s a prefix
operation, K will be incremented by 1, then assigned to L.
Therefore, K = 3 and L = 3.
b) X = 25;
Y = - - X;
Explanation:
X is assigned with the value 25. Because it’s a prefix
operation, K will be decremented by 1, then assigned to L.
Therefore, K = 24 and L = 24.
(ii) Postfix Increment / Decrement
Examples:
a) M = 20;
N = M + +;

Trainer: Simon Wamwea.


Contact 0717871829 15
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
Explanation:
M is assigned with the value 20. Because it’s a postfix
increment operation, N is assigned the value in M, then M is
incremented by 1. Therefore, N = 20 and M = 21.
b) P = 36;
Q = X- -;
Explanation:
P is assigned with the value 36. Because it’s a postfix
decrement operation, Q will be assigned the value of P, then,
Q will be decremented by 1.Therefore, Q = 36 and P = 35.

(ii) Relational Operators / Operators


< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to

The hierarchy of relational / comparison operators are given below:


(i) <, <=, >, >= Evaluate from left to right, then
(ii) == , != Evaluate from left to right

Example 1: Less than


int x = 10;
int y = 15;
Explanation:
x<y true
y<x false
Example 2: Less than or equal to
int x = 10;

Trainer: Simon Wamwea.


Contact 0717871829 16
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
int y = 15;
Explanation:
x<=y true
y<=x false
Example 3: Greater than
int x = 10;
int y = 15;
Explanation:
x>y false
y>x true
Example 4: Greater than or equal to
int x = 10;
int y = 15;
Explanation:
x>=y false
y>=x true
Example 5: Equal to
int x = 10;
int y = 15;
int z = 10;
Explanation:
x = = y; false
x = = z; true
Example 6: Not Equal to
int x = 10;
int y = 15;
int z = 10;
Explanation:
x != y true
x != z false

Trainer: Simon Wamwea.


Contact 0717871829 17
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
(iii) Logical Operators
Logical operators combine the results of one or more expressions and results in what is called
a logical expression. After testing the condition, they return the logical status (true or false as
the net result).
Logical operators include:
a) NOT !
b) AND &&
c) OR ||

a) NOT !
It returns true if the expression is false and true if the result is false.
Syntax:
if(!(x<y))
Explanation:
the statement will only be executed if x is not less than y.
b) AND &&
Returns true if both conditions being tested are true and false if either of
the conditions being tested returns a false.
Syntax:
if((x>=0) && (x<=100))
Explanation:
The statement will only be executed if x is greater than or equal to 0 and
at the same time less than or equal to 100 i.e. x is between 0 and 100.
c) OR ||
Returns false if both conditions are false, and returns true if either of the
conditions returns a true.
Syntax:
(condition1) | | (condtion2)
Example 1:
if((x<0) | | (x>100))
Explanation:

Trainer: Simon Wamwea.


Contact 0717871829 18
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
The statement will be executed if either x is less than 0 or if x is greater
than 100.
Example 2:
if((x>10) | | (b<700))
This statement will be executed if a, is greater 10, OR , b is less 700
OR if a, is greater than 10, and b is less than700.

N.B: For OR | | , only one condition has to be true(or both). The only time the overall result
becomes false is only when both conditions are false.

Conclusion
Operator precedence determines the grouping of terms in an expression. The associativity of an
operator is a property that determines how operators of the same precedence are grouped in the
absence of parentheses. This affects how an expression is evaluated. Certain operators have
higher precedence than others; for example, the multiplication operator has higher precedence
than the addition operator:

For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof 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

Trainer: Simon Wamwea.


Contact 0717871829 19
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
Category Operator Associativity
Comma , Left to right

SUMMARY
In this lesson, you have learned how to program, compile, link, and execute your first C++
program. This lesson also has informed you on C++ program constructs.

Q&A
1. Can I ignore warning messages from my compiler?
2. How does an interpreted language differ from a compiled language?
3. What are runtime errors, and how are they different from compile-time
4. errors?
5. What does #include do?
6. What is the difference between // comments and /* comments?

WORKSHOP
The Workshop provides quiz questions to help you solidify your understanding of
the material covered and exercises to provide you with experience in using what you’ve
learned.

Also, we have introduced the basic parts of a simple C++ program. In retrospect, we have
learnt what main( ) is, got an introduction to namespaces, and learned the basics of input and
output.
Therefore, you are able to use most of the constructs(variables, constants, statements,
operators etc.) of C++ language in every program you write.

QUIZ
1. Describe the major parts of a C++ program.
2. Differentiate between Boolean and character literals a used in C++ programming.
3. Define an identifier . what are the rules followed to create it in C++ programming
language.
4. Give a summary of three data types used in C++ programs.
5. What are runtime errors, and how are they different from compile-time errors?
6. Calculate value of each of the following C++ statements (show you’re working).

Trainer: Simon Wamwea.


Contact 0717871829 20
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP
b = 12 + 4 – 7 * 9 % 6 + 12/
c = 13 – (8%5) -9%7 – 3 + 6 * 2
7. Expound on the purpose of using namespace std in C++
8. What is the difference between an interpreter and a compiler?.
9. Differentiate between header file and inbuilt function as used C++ programming.
10. Expound on four types of operators used in C++ programming.
11. Expound on the stages of creating a program in OOP.
12. What is the use of preprocessor directive #include<iostream>?
EXERCISE
1. Write a C++ program that accepts two numbers from a user through the keyboard.
The program computes both the sum and average of the numbers and displays the
answers on the screen.
2. As the lead programmer in an architectural firm, you have been requested to write
a C++ program. The program should accept user input, compute the area and
perimeter of a rectangle and display the results on the screen.
3. A C++ program is required that calculates the area and circumference of a circle
with a diameter of 14cm.
4. Enter this program and compile it. Why does it fail? How can you fix it?
#include <iostream>
main( )
{
cout << Are you are a C++ Programmer?"
{
5. Fix the bugs in question 4 and recompile, link, and run it.

Trainer: Simon Wamwea.


Contact 0717871829 21
PROGRAM: DCS/DSE/DIT/DBIT
UNIT: Object Oriented Programming
SEMESTER: January-April 2022
TOPIC 3: Language Structures/Constructs of OOP

Reference
Cs.utah.edu. 2022. Programming - Object Oriented Programming. [online] Available at:
<https://www.cs.utah.edu/~germain/PPS/Topics/oop.html> [Accessed 9 February 2022].
Coursehero.com. 2022. 1. oop with c++.pdf - Paper Code: Lesson no: 1 Author: Pooja Chawla
Paper Name: OOP with C+ Lesson Name: Introduction of OOP Vetter: Prof. Dharminder | Course Hero.
[online] Available at: <https://www.coursehero.com/file/107998793/1-oop-with-cpdf/> [Accessed 12
February 2022].
Chortle.ccsu.edu. 2022. Object Oriented Programming. [online] Available at:
<https://chortle.ccsu.edu/java5/Notes/chap30/ch30_2.html> [Accessed 12 February 2022].

Trainer: Simon Wamwea.


Contact 0717871829 22

You might also like