0% found this document useful (0 votes)
18 views27 pages

CSC 283

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views27 pages

CSC 283

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture – 3

Overview of C++ Programming


Introduction to C++

Data Types

Constants , Variables
& Operators

CONTENTS Control Structure

2
Structure of a C++ Program
return type function name argument list

• Every C++ program must


have a main function

• Source files are saved


with .cpp extension

function body
3
Data Types
Type Size (Bytes) Format Specifier Value Range

char 1 %c 0 to 255
(or -128 to 127)
int 4 %d -32,768 to 32,767
(-2^15 to 2^15-
1)
float 4 %f 1.2E-38 to 3.4E+38
(6 decimal places)
double 8 %lf 2.3E-308 to 1.7E+308
(15 decimal places)
4
Data Types (Cont.)
Data Types

Prmitive Derived User Defined

char array struct

int pointer union

float function enum

double
5
Reserved Words and Identifiers

Reserve Words
•Word that has a
specific meaning in C++ Identifiers
•Known as keyword •Word used to name and refer
•Ex: int, retrun, main to a data element
•Ex: functions, variables

6
Valid Identifiers
Consists of letters, digits, or underscores only
01 ✔ myName_123
× myName#
Begins with a letter or underscore symbol
02 ✔ myName, MyName, _myName
× 123_MyName
Cannot be a C reserved word
03 × main, for
✔ mainName
04 At least one letter or underscore, should not be empty

Identifiers are Case Sensitive: myName≠MyName≠MYNAME 7


Variables & Constants

Constants
Variables
•A value that can not be
•Is a memory location where
altered throughout the
a value can be stored
program
•Value can change
•Ex: 1, 2, 3
during program
#define PI 3.1416
execution
•Ex: int a = 3;

8
Symbolic Constants
□ In C++, symbolic constants are identifiers that
represent constant values throughout the program.
These constants remain unchanged during the
execution of the program.

In C++ program we can define constants in two ways as


shown below:

□Using enum keyward for only integers.


□Using a const keyword
9
Symbolic Constants

Syntax Syntax

10
Dynamic Initialization of Variables
□ In C++, Dynamic initialization refers to the process
of initializing variables during runtime rather than at
compile time.
#include <iostream> using
namespace std;

int main() { int a, b;


cout << "Enter two numbers: "; cin >>
a >> b;

int sum = a + b; // Dynamically initializing sum based on user


input cout << "Sum is: " << sum << endl;

return 0;
}
In this example, a, b, and sum are dynamically initialized
based on user input at runtime. 11
Dynamic Initialization of Variables
□ In C++ a reference variable is an alias for an existing variable. Once a
reference is initialized with a variable, it becomes an alternative name for
that variable. Any changes made to the reference will affect the original
variable.
#include <iostream> using namespace std;

Syntax int main() { int x = 10;


int &ref = x; // ref is a reference to x

cout << "x = " << x << endl; // Output: x = 10 cout << "ref = " << ref <<
endl; // Output: ref = 10

ref = 20; // Changing ref changes x cout << "After modifying ref:" << endl;
cout << "x = " << x << endl; // Output: x = 20 cout << "ref = " << ref <<
endl; // Output: ref = 20

return 0;
}

12
Reference Variables
□ A reference in C++ is an alias for another variable. Once a reference is
initialized with a variable, it becomes an alternative name for that variable,
meaning any operation performed on the reference is actually performed
on the original variable.
#include <iostream>
using namespace
std;
Syntax
int main() { int
a = 10;
int &ref =
a; // ref is a
reference to
the variable a

ref = 20; //
Modifies a 13
through ref
Memory Management Operators
□ In C++ provides two key memory management operators for dynamically
allocating and deallocating memory during program execution: new and
delete. These operators allow you to manage memory manually, which is
crucial for efficient use of resources, especially when dealing with large
Syntax amounts of data. #include <iostream> using namespace std;

int main() {
int *ptr = new int(5); // Allocate memory and initialize to 5 cout << "Value: "
<< *ptr << endl; // Output: 5
delete ptr; // Deallocate memory

// Allocating memory for an array of integers int *arr = new


int[3];
Syntax arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
for (int i = 0; i < 3; i++) {
cout << arr[i] << " "; // Output: 1 2 3
}
delete[] arr; // Deallocate the array memory
✔ The new operator is used to allocate memory dynamically.
✔ The delete operator is used to free memory that was previously return 0; 14
✔ allocated with new }
Type Casting

Type cast

operator

□ Type cast – a way of


changing a value of one type
to a value of another type

15
C++ Operators

Arithmatic Bitwise
+, -, *, /, % &, |, ~, ^, <<, >>

Relational Assignments
==, !=, <, <=, >, >= =, +=, -=, *=, /=

Logical Special
!, &&, || ++, --, ?, &, *, [], ., ->

16
Scope Resolution Operators
□ The scope resolution operator (::) in C++ is used to define and access
identifiers (such as variables, functions, or classes) that are located in
different scopes. It helps resolve ambiguity when the same name is used in
different scopes (global, class, or namespace).
Accessing the global
Variable

Syntax
#include<iostream> using namespace std;

int x = 10; // Global variable int main() {


int x = 20; // Local variable
cout << "Local x: " << x << endl; // Outputs
20
cout << "Global x: " << ::x << endl; // Outputs 10 using the scope resolution operator
return 0;
}
17
Scope Resolution Operators Cont..
Accessing
Namespaces

#include<iostream>

namespace first { int x = 10;


The scope resolution operator is used to access variables,
} functions, or classes inside a namespace to avoid conflicts with
global identifiers.

namespace second { int x = 20;


}

int main() {
std::cout << "first::x = " << first::x << std::endl; // Accessing x from namespace first
std::cout << "second::x = " << second::x << std::endl; // Accessing x from namespace second
return 0;
}

18
Control Structures

□Control structures are fundamental building blocks in any programming language,


including C++. They determine the flow of control in a program, allowing it to make
decisions, repeat actions, and choose between alternative courses of action.

□In C++, control structures are :


I. Selection (Decision-Making) Control Structure
II. Repetition (Looping) Control Structure

19
Selection Control Structures

□Selection structures allow the program to make decisions based on


conditions.

I. if Statement
II. if-else Statement
III. else if Ladder
IV. switch Statement

20
21
The switch

Consists of a series of case An optional


labels default case at the end

Useful when variable or Swithch reduces


expression is tested for if's/if-else
multiple values ladder

22
23
Repetition(Loop) Control Structures

□Repetition structures, also known as loops, allow you to repeat a block of


code multiple times.

I. while
II. do-while
III. for

24
The For Loop
1. How many times the
loop body execute?

2. What will be the


output?

25
The Do-While Loop

26
Comparison of 3 loops

27

You might also like