Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 16

PROGRAMING FUNDAMENTALS

~ Constants in C++: -
1. What is a Constant?

· Constants refer to fixed values that the program may not alter and they are called literals.

· Constants can be of any of the basic data types and can be divided into Integer Numerals,
Floating-Point Numerals, Characters, Strings and Boolean Values.

· Constants are treated just like regular variables except that their values cannot be modified
after their definition.

2. Defining Constants:
· There are two simple ways in C++ to define constants:

1. Using #define preprocessor.

2. Using const keyword.

· It is Good Programming practice to define constants in CAPITALS.

3. The #define Preprocessor:


· Following is the form to use #define preprocessor to define a constant: Using #define

#define identifier value

· Any occurrence of identifier in the code is interpreted as replacement, where replacement is


any sequence of characters.

· Replacement performed by the preprocessor, before the program is compiled, thus cause
sort of blind replacement.

· Validity of the types or syntax involved is not checked in any way.


· Do not require semicolons (;) at the end; If a semicolon is included in the line, it is part of
the replacement sequence and included in all replacements.

4. Named Constants:
· Concept: Literals may be given names that symbolically represents them in a program.

· A named constant is like a variable, but its content is read-only, and cannot be changed
while the program is running.

· Use const prefix to declare constants with a specific type as follows:

const type variable = value;

· Sometimes, it is convenient to give a name to constant value

· A const object is subject to the scoping rules for variables, whereas a constant created using
#define is not.

~ EXPRESSION:
1. What is an Expression?
· Expressions are sequences of operators and operands that that specifies a computation.

· Expressions are used for one or more of these purposes:

1. Computing a value from the operands

2. Designating objects or functions.

2. Types of Expression:
Expressions may be of one of following types

· Integral Expression: A mathematical expression involving one or more integral (all operands
are integer) and produces result in integer form.

· Floating Expression: A mathematical expression that all operands of float type. The result
will be float also.

· Mixed Expression: An expression that has operands of different data types is called a mixed
expression.

3. Evaluating Expression:
When evaluating an operator in a mixed expression:

· If both operands have same data type then result will be of operand type.

· If the operator has different types of operands then the result would of higher data type
used in expression.

· For example one is an integer, one is float and the third is a double, then the result would be
a double number.

~ Type Casting in C++:


1. What is Type Casting?
· Type casting is a way to convert a variable from one data type to another data type.

· For example, if you want to store a long value into a simple integer then you can type cast
long to int.

· You can convert values from one type to another explicitly using the cast operator as
follows:

(type_name) expression

int sum = 17, count =

5; double mean;

mean = (double) sum / count;

2. Type Casting - Types:


Type casting is of two types
· Implicit Type Casting: conversion performed by the compiler automatically.

· Explicit Type Casting: Conversion performed via in the form of unary type-casting
operator in the

(new-type) operand

new-type (operand)

Example: -

double x = 10.3;

int y;

y = int (x);

y = (int) x;

~ Implicit Type Casting:


· Assignment of an int value to a double variable, automatically casts to double (e.g.,
from 1 to 1.0).

· Assignment of a double value to an int variable, automatically casts to an int value


(e.g., from 1.2 to 1).

· The fractional part would be truncated and lost in case conversion from float or
double to int.
~ Type Promotion:
1. What is Type Promotion?
· Special case of implicit type conversion.

· In type promotion, the compiler automatically expands the binary representation of objects
of integer or floating-point types.

· Promotions are done prior to arithmetic and logical operations in order to make operations
possible, or more efficient if the ALU can work with more than one type.

· C++ perform such promotion for objects of boolean, character, wide character and short
integer types which are promoted to int, and for objects of type float, which are promoted to
double.

· Promotions never lose precision or modify the value stored in the object.

int x = 3; Output=6.5

double y = 3.5;
cout<<(x+y);

· Integer promotion is the process by which values of integer type. "smaller" than int or
unsigned int are converted either to int or unsigned int.

int sum, i = 17;

char c = 'c'; /* ascii value is 99

*/ sum = i + c;

cout<<sum; Output=116

· Compiler is doing integer promotion and converting the value of 'c' to ascii before
performing actual addition operation.

~ Explicit Type casting:

~ Escape Sequence:
1. What is Escape Sequence?
· An escape sequence is a sequence of characters that does not represent itself when used
inside a character or string literal, but is translated into another character or a sequence of
characters that may be difficult or impossible to represent directly.

· Syntax is as follow:

cout<<“My name on next line\n Atif”;


~ Quadratic Equation:

~ Discriminant:
b2 - 4ac /2a

= b*b - 4*a*c /2 *a Incorrect answer

Solution:

= (b*b - 4*a*c) /(2 *a) Correct answer


~ Input Stream in C++:
1. cin in c++:
· Concept : the cin object can be used to read data typed at the keyboard.

· cin is used together with the extraction operator, which is written as >>. This operator is
then followed by the variable where the extracted data is stored.

#include <iostream>

using namespace std;

main ()

int i;

cout << "Please enter an integer value: ";

cin >> i;

cout << "The value you entered is " << i;

~ cin and the Extraction Operator >>:


· Suppose payRate is double variable.

· cin >> payRate;

· The extraction operator >> is binary and thus takes two operands.

· The left-side operand must be an input stream variable, such as cin. And right hand side is a
variable to store the data.

cin >> payRate >> hoursWorked;

Equivalent to

cin >> payRate;

cin >> hoursWorked;

· White space or carriage return are equivalent to segregate the two inputs.
~ Input Stream in C++:
1. cin without >> operator:
· The >> operator may be used when you simply want to read the next non- blankspace
characters entered by the user into a character or character array.

· Any printable characters that follow the first space will be ignored and will not be stored in
the variable.

· if, for example, you wish to obtain a whole sentence from the user that includes spaces. In
that case, you would be better served by using the cin member functions get or getline.

2. Using cin.get:
· The unformatted get member function works like the >> operator with one exceptions.

· The get function includes white-space characters, whereas the extractor excludes white
space.

· A variation of the get function specifies a buffer address and the maximum number of
characters to read. This is useful for limiting the number of characters sent to a specific
variable, as in example:

· Example:

using namespace std;

#include <iostream>

main()

char line[25];

cout << " Type Line and enter\n>";

cin.get( line, 25 );

cout << ' ' << line;

In this example, you can type up to 24 characters and a terminating

character. Any remaining characters can be extracted later.


3. Using cin.getline:
· The getline member function is similar to the get function.

· Both functions allow a third argument that specifies the terminating character for input.

· The default value is the newline character.

· Both functions reserve one character for the required terminating character.

· However, get takes the input stream character by character while getiline take the input
steram line by line.

· Example:

using namespace std;

#include<iostream>

void main()

char line[100];

cout<<"Type Line terminated by ‘t’ \n>";

cin.getline( line, 100,’t’ );

cout << line;

In this example, if user enter Pakistan, the output will be Pakis. It will

display characters up to ‘t’

~ Use of Operator:
1. Problem Statements:
· Calculate the average age of a class of ten students. Prompt the user to enter the age of each
student.

Problem Analysis:
We have to calculate the average age of ten students so we will take the ages of ten students
from the user. Ten variables are required to store the ages, one variable for each student’s age. We
will take the ages of students in whole numbers (in years only, like 10, 12, 15 etc), so we will use the
variables of data type int.

2. Problem Statements:
· Write a program that takes a four digits integer from user and shows the digits on the
screen separately i.e. if user enters 7531, it displays 1,3,5,7 separately.

Problem Analysis:
We know that when we divide a number by 10, we get the last digit of the

number as remainder. For example when we divide 2415 by 10 we get 5 as remainder.

We will use this logic in our problem to get the digits of the number. First of

all, we declare two variables for storing number and the digit.

In our program we will use modulus operator ( % ) to get the remainder. We will use the same login
to solve above problem.

3. Problem Solution:
· Lets Consider User enters number 1234

Number = 1234

· Take the remainder of the above number after dividing by 10

1234 % 10 = 4

· Remove last digit (truncate due to integer)

1234/10 = 123.4

· Take the remainder of new number 123

123 % 10 = 3

· Remove last digit (Truncate due to integer)

123/10 = 12.3

· Take the remainder of new number 12

12 % 10 = 2
· Remove last digit (Truncate due to integer)

12/10 = 1.2

4. Code in C++:
#include<iostream.h>

using namespace std;

main ( )

int number,digit;

cout << “Please enter a 4 digit integer :”;

cin >> number;

digit = number %10;

cout <<digit << ‘ , ’;

number = number / 10;

digit = number % 10;

cout <<digit << ‘ ,’;

number = number / 10;

digit = number % 10; c

out <<digit << ‘ ,';

You might also like