CS32080 - Computer Programming: E19-S3 - Nov2022
CS32080 - Computer Programming: E19-S3 - Nov2022
E19-S3 | Nov2022
Dept. of Mechanical Engineering
Faculty of Engineering | South Eastern University of Sri Lanka
M.N.M. Aashiq
1
Topics
1. Philosophy of object-orientation (2 hrs) 4. Object-oriented application development (2h)
1. Instance creation and scope control.
1. Abstraction
2. Data structures and iterable collections.
2. Encapsulation and information-hiding.
5. I/O and Database handling (3 hrs)
3. Separation of behaviour and implementation. 1. Stream based I/O and object serialization
4. Division of a system into manageable components 2. String and file manipulation
5. Reuse and simple interfaces 3. Database Programming
2. Introduction to C++ program constructs (2h) 6. OO analysis and design using UML.(2 hrs)
1. C++ programming language and API 1. Drawing UML class, package and component
2. Variables, Data Types and Controls diagrams.
3. Arrays
2. User-centred design and use cases
3. Software behaviour representation with
4. Procedures and Functions
sequence diagrams, state machines, and
5. Program Control Flow activity diagrams.
6. Exception Handling 7. Advanced topics. (2hrs)
3. Object-oriented programming (2 hrs) 1. Design patterns
1. Definition and use of classes 2. Evaluation of designs
2. Subclasses, inheritance and class hierarchies. 3. Domain-specific modelling.
3. Polymorphism 4. Architectural patterns
2
What is anIdentifier?
• An identifier is the name to denote labels, types, variables, constants
or functions, in a C++ program.
• C++ is a case-sensitive language.
• Work is not work
• Identifiers should bedescriptive
• Using meaningful identifiers is a good programming practice
3
Identifier
• Identifiers must beunique
• Identifiers cannot be reserved words(keywords)
• double main return
• Identifier must start with a letter or underscore, and be followed by zero or
more letters (A-Z, a-z), digits (0-9), or underscores
• VALID
age_of_dog _taxRateY2K
PrintHeading ageOfHorse
• NOT VALID
age# 2000TaxRate Age-Of-Dog main
4
2.1 Data types
5
C++ Primitive Data Types
Primitive types
integral floating
unsigned
6
Premitive Data Types in C++
• Integer Types
• represent whole numbers and their negatives
• declared as int, short, orlong
• Character Types
• represent single characters
• declared as char
• Stored by ASCII values
• Boolean Type
• declared as bool
• has only 2 valuestrue/false
• will not print out directly
• Floating Types
• represent real numbers with a decimalpoint
• declared as float, or double
• Scientific notation where e (or E) stand for “times 10 to the ”
(5.5e-6)
7
Samples of C++ DataValues
int sample values
4578 -4578 0
bool values
true false
8
9
//C++ program to sizes of datatypes
#include<iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char)
<< " byte" << endl;
cout << "Size of int : " << sizeof(int)
<< " bytes" << endl;
cout << "Size of short int : " << sizeof(short int)
<< " bytes" << endl;
cout << "Size of long int : " << sizeof(long int)
<< " bytes" << endl;
cout << "Size of signed long int : " << sizeof(signed long int)
<< " bytes" << endl;
cout << "Size of unsigned long int : " << sizeof(unsigned long int)
<< " bytes" << endl;
cout << "Size of float : " << sizeof(float)
<< " bytes" <<endl;
cout << "Size of double : " << sizeof(double)
<< " bytes" << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t)
<< " bytes" <<endl;
return 0;
}
10
2.2 What is aVariable?
• A variable is a memory address where data can be stored and
changed.
• Declaring a variable means specifying both its name and its data type.
11
What Does a
Variable Declaration Do?
• A declaration tells the compiler to allocate enough memory to hold a
value of this data type, and to associate the identifier with this
location.
• int ageOfDog;→
• char middleInitial; →
• float taxRate;→
12
Variable Declaration
• All variables must be declared beforeuse.
• At the top of theprogram
• Just before use.
• Commas are used to separate identifiers of the same
type.
int count, age;
• Variables can beinitialized to a starting value when
they are declared
int count = 0;
int age, count = 0;
13
What is an Expression in C++?
• An expression is a valid arrangement of variables, constants, and
operators.
• In C++, each expression can be evaluated to compute a value of a
given type
14
Assignment Operator
• An operator to give (assign) a value to a variable.
• Denote as ‘=‘
• Only variable can be on the left side.
• An expression is on the right side.
• Variables keep their assigned values until changed by another
assignment statement or by reading in a new value.
15
Assignment Operator Syntax
• Variable = Expression
• First, expression on right isevaluated.
• Then the resulting value is stored in the memory location of Variable on left.
16
Assignment Operator Mechanism
• Example:
0
int count = 0;
int starting; 12345 (garbage)
starting = count + 5;
• Expression evaluation:
• Get value of count:0
• Add 5 toit.
• Assign to starting
5
17
References
1. www.geeksforgeeks.org
2. www.rooksguide.files.wordpress.com
3. www.iwan.staff.gunadarma.ac.id
4. www.open.library.ubc.ca
5. www.no1tutorial.com
18
Thank you
19