OBJECT ORIENTED PROGRAMMING (OOP) WITH C++
AIUB. SPRING 2018
Dr. Mahbubul Syeed
Associate Professor, Department. of CS, AIUB
mahbubul.syeed@aiub.edu
www.msyeed.weebly.com
CONTENTS
Meet the teacher :)
Course Outline.
Evaluation Criteria.
Introduction to the world of Programming with C++.
MEET THE TEACHER
Ahsanullah University
Publications: ~30
Tampere University of
Technology Research: SW development process,
practices, project evolution, SW
Packet Video ecosystem, automation of project
corporation analysis
Finnish Geospatial
Research Institute PhD in Software Engineering
IUT, BUET, TUT
Several Commercial and
Dr. Mahbubul Syeed
Research based SW
Associate professor, Dept. of CS, AIUB
EVALUATION CRITERIA
Theory:
Attendance (15%).
Quiz (15%).
Lab:
Assignments and Report (10%).
Attendance (15%).
Class Performance (10%).
Class Performance (15%).
Examination (50%).
Assignments (15%).
Project (10%).
Examination (45%).
WHY DO WE PROGRAM… ??
We do…
Input and Output.
Calculate and compute.. E.g.,
mathematical operations (+, -, x, /, %, ….)
logic operations (&, II, ….)
Comparisons (>, <, >=, <=, !=, …..)
recurring event management (display information after every 10 sec., updating clock time,….)
Data storage and manipulation (store all course GPA in your program and calculate the CGPA, …)
Communication..
….. Many more …
PROGRAMMING LANGUAGE
WHAT IS IT???
A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks.
For example, C, C++, JAVA, JAVA Script, …….
For example, If you want to print a line of text
“I Love Programming!”
using C language you need to write the
following:
#include <iostream>
int main()
{
std::cout <<”I Love Programming!”;
return 0;
}
WHICH LEVEL IS C++ LANGUAGE BELONGING TO?
Programming language
Low Level languages:
Low level languages provides nothing other than access to the machines basic
instruction set.
Example:Assembler
Middle Level languages:
Middle level languages don’t provide all the built-in functions found
in high level languages, but provides all building blocks that we need
to produce the result we want. Examples: C, C++
High Level languages:
High level languages provide almost everything that the programmer might
need to do as already built into the language. Example: Java, Python
WHICH LANGUAGE FAMILY C++ BELONGS TO?
Structure / procedural oriented language:
In this type of language, large programs are divided into small programs called functions
Prime focus is on functions and procedures that operate on the data
Data moves freely around the systems from one function to another
Program structure follows “Top Down Approach”
Examples: C, Pascal, ALGOL and Modula-2
Object oriented language:
In this type of language, programs are divided into classes and objects
Prime focus is in the data that is being operated and not on the functions or procedures
Data is hidden and cannot be accessed by external functions
Program structure follows “Bottom UP Approach”
Examples: C++, JAVA and C# (C sharp)
Non structure oriented language:
There is no specific structure for programming this language. Examples: BASIC, COBOL, FORTRAN
ORIGINS OF C++
C++ extensions of C were first invented by Bjarne Stroustrup in 1979 and it was called “C with
classes”, became C++ in ’83.
Greatly influenced by Simula67
First revision 1985
Second revision 1990
Third revision 1994 ( ISO standardization)
OBJECT ORIENTED PROGRAMMING
An approach that provides a way of modularizing programs by
creating partitioned memory area for both data & functions that can be used
as templates for creating copies of such modules on demand.
DIFFERENCE BETWEEN
PROCEDURAL PROGRAMMING AND OOP
Global Data Global Data
Function - 1 Function - 2 Function - 3
Local Data Local Data Local Data
Fig: Relationship of data and functions in procedural programming
DIFFERENCE BETWEEN
PROCEDURAL PROGRAMMING AND OOP
Data Data
Functions Functions
Object A Data Object B
Functions
Object C
Fig: Organization of data and functions in OOP
NEED OF OOP
Here is the fate of the US defense software projects undertaken in the 1970s
• 50% of the software were never delivered
• one third of what delivered was never used
• Finally, only 2% were used as delivered, without being subjected to any changes.
BASIC CONCEPTS/COMPONENTS OF OOP
Class: A class is a template definition consisting of attributes (or variables) and methods (or
functions) to present a particular type of objects.
Attributes / variables: Describe the properties that the objects should have.
Methods / functions: Describes or defines the actions / functions that those group of objects
should perform
Object: Objects are instances of a class.
EXAMPLE: A PERSON CLASS AND ITS OBJECT
class Person { Creating objects:
private:
string firstName; Person abdullah, aPerson;
string lastName;
string dob; //set the detail for person objects
double age; abdullah. setFirstName(“Mohammad”);
string gender; abdullah. setLastName(“Abdullah”);
public: aPerson.setFirstName(“Shah Rukh”);
void setFirstName(string name); aPerson. setLastName(“Khan”);
void setLastName(string name); aPerson. setDetail(“19.12.60”, 50, “Male”);
void setDetail(string dob, double age, string gen);
string setFirstName(); // accessing individual person information
string getLastName();
string getdob(); cout << “DOB:” << aPerson.getdob();
}; cout << “Name: ” << abdullah.getFirstName();
BASIC CONCEPTS/COMPONENTS OF OOP
Data abstraction:
The wrapping of data & functions together is known as abstraction.
Data Encapsulation:
Prevention of accessing of data of a class for other class objects is known as data
encapsulation.
BASIC CONCEPTS/COMPONENTS OF OOP
Inheritance (Is-A relationship)
A mechanism that helps objects of one class to inherit properties from objects of other
class.
Inheritance supports re-usability.
Polymorphism:
Polymorphism is derived from 2 greek words: poly (many) and morphs (forms). So
polymorphism means many forms.
A same action can cause different reaction from different objects.
RECOMMENDED SOURCES
Online:
1. http://www.cplusplus.com/doc/tutorial/
Basic Texts
Teach Yourself C++, 3rd Edition, Herbert Schildt.
The C++ Complete Reference, 4th Edition, Herbert Schildt.
Reference Texts/ Weblinks:
C++ How to Program, 4th Edition, Deitel and Deitel.
The C++ Programming Language, Special 3rd Edition, Bjarne Stroustrup
Thinking in C++, Volume One, 2nd Edition. Bruce Eckel.
Downloadable from http://www.BruceEckel.com.
The C++ Programming Language by Bjarne Stroustrup
C++ Primer Plus, 5th Edition by Stephen Prata
Essential C++ (The C++ In-depth Series) by Stanley B. Lippman
EDITORS AND COMPILERS
Online:
1. C++ Shell [http://cpp.sh/]
2. OnlineGDB [https://www.onlinegdb.com/online_c++_compiler]
3. CodeChef [https://www.codechef.com/ide]
4. JDOODLE [https://www.jdoodle.com/online-compiler-c++]
IDE Platform How to install and use (follow the link bellow)
http://www.cplusplus.com/doc/tutorial/introduction/codebl
Code::blocks Windows/Linux/MacOS
ocks/
http://www.cplusplus.com/doc/tutorial/introduction/visuals
Visual Studio Express Windows
tudio/
http://www.cplusplus.com/doc/tutorial/introduction/devcp
Dev-C++ Windows
p/
STRUCTURE OF A PROGRAM
STATEMENT
1 int x;
2 x = 5;
3 std::cout << x;
int x;
is a declaration statement. This particular declaration statement tells the compiler that x is a variable that holds an integer (int)
value. In programming, a variable provides a name for a region of memory that can hold a value that can vary. All variables in a
program must be declared before they are used. We will talk more about variables shortly.
x = 5;
is an assignment statement. It assigns a value (5) to a variable (x).
std::cout << x;
is an output statement. It outputs the value of x (which we set to 5 in the previous statement) to the screen.
EXPRESSION
Expressions can involve
values (such as 2),
variables (such as x),
operators (such as +) and
functions (which return an output value based on some input value).
They can be
singular (such as 2, or x), or
compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3)).
For example, the statement x = 2 + 3; is a valid assignment statement.
The expression 2 + 3 evaluates to the value of 5. This value of 5 is then assigned to x.
FUNCTION
In C++, statements are typically grouped into units called functions.
A function is a collection of statements that executes sequentially.
Every C++ program must contain a special function called main.
When the C++ program is run, execution starts with the first statement inside of
function main.
LIBRARY
A library is a collection of precompiled code (e.g. functions) that has been “packaged up”
for reuse in many different programs.
Libraries provide a common way to extend what your programs can do.
For example, if you were writing a game, you’d probably want to include a sound library and
a graphics library.
Example libraries: iostream, fstream, cmath, cstring, algorithm, complex
(see the full list here: http://en.cppreference.com/w/cpp/header )
A COMPLETE C++ PROGRAM
Line 1 a preprocessor directive telling the compiler that we would like to add the
1 #include <iostream> contents of the iostream header to our program. The iostream header allows us to
2 access functionality from the iostream library, e.g., read/write text in the screen.
3 int main()
Line 2 is blank, and is ignored by the compiler.
4 {
5 std::cout << "Hello world!"; Line 3 declares the main() function. Every program must have a main() function.
6 return 0;
7 } Lines 4 and 7 tell the compiler which lines are part of the main function.
Line 5 is the first statement (you can tell it’s a statement because it ends with a semicolon).
It is an output statement. std::cout is a special object that represents the console/screen.
The << symbol is an operator (much like + is an operator in mathematics) called the output operator.
std::cout understands that anything sent to it via the output operator should be printed on the screen.
Line 6 is called a return statement. When an executable program finishes running, the main() function sends a value back to the
operating system that indicates whether it was run successfully or not.
BASIC COMPONENTS OF A PROGRAM
COMMENT
A comment is a line (or multiple lines) of text that are inserted into the source code to explain what the code is
doing. In C++ there are two kinds of comments
The // symbol begins a C++ single-line comment, which tells the compiler to ignore everything to the end of the line.
1 std::cout << "Hello world!" << std::endl; // Everything from he
re to the right is ignored.
The /* and */ pair of symbols denotes a C-style multi-line comment. Everything in between the symbols is ignored.
1 /* This is a multi-line comment.
2 This line will be ignored.
3 So will this one. */
STD::COUT
the std::cout object (in the iostream library) can be used to output text to the console. As a reminder,
here’s our Hello world program:
To print more than one thing on the same line, the output operator (<<) can be used multiple times. For
example:
1 #include <iostream>
2 1 #include <iostream>
3 int main() 2
4 { 3 int main()
5 std::cout << "Hello world!"; 4 {
6 return 0; 5 int x = 4;
7 } 6 std::cout << "x is equal to: " << x;
7 return 0;
8 }
STD::COUT
What would you expect this program to print?
1 #include <iostream>
2
3 int main()
4 {
5 std::cout << "Hi!";
6 std::cout << "My name is Alex.";
7 return 0;
8 }
STD::ENDL
If we want to print things to more than one line, we can do that by using std::endl. When used with std::cout,
std::endl inserts a newline character (causing the cursor to go to the start of the next line).
For example:
1 #include <iostream>
2
3 int main()
4 {
5 std::cout << "Hi!" << std::endl;
6 std::cout << "My name is Alex." << std::endl;
7 return 0;
8 }
STD::CIN
std::cin is the opposite of std::cout -- whereas std::cout prints data to the console using the output operator
(<<), std::cin reads input from the user at the console using the input operator (>>). Now that you have a
basic understanding of variables, we can use std::cin to get input from the user and store it in a variable.
1 #include <iostream>
2
3 int main()
4 {
5 std::cout << "Enter a number: "; // ask user for a number
6 int x; // no need to initialize x since we're going to overwrite that value on the very next line
7 std::cin >> x; // read number from console and store it in x
8 std::cout << "You entered " << x << std::endl;
9 return 0;
10 }
11
ASSIGNMENT (LAB)
Write a program in C++ to perform the followings:
Take 5 integers as input (from keyboard) using for-loop.
Use Array to input the values. NO NEED TO SUBMIT IT!!
Find maximum and minimum among them (using if-else statement).
Output the result.
Sample input and output:
Enter 5 numbers: 10 9 25 8 3 How to submit:
1. Mail the solution as an email attachment.
Maximum is: 25 (.cpp format)
Minimum is: 3 2. Email subject: AssignmentPL2_1
3. Write your name and id in Email body.
*** Subject should be the exact one as given, otherwise your
assignment will not be received**