Unit 1 - Object Oriented Programming / C++
Unit 1 - Object Oriented Programming / C++
QF 002/1/1
UNIT 1
OBJECTIVES
General Objective
Specific Objectives
Describe the basic concept of the C and C++ programming Identify the differences between C and C++ programming Explain the evolution of C++ programming Explain the structure of C++ programming
QF 002/1/2
INPUT
1.0
Introduction
A program is a collection of instructions that tell the computer what to do. A program is written in a programming language and is converted into the computers machine language by software called assemblers, compilers and interpreters.
note
QF 002/1/3
1.1
Introduction To C Programming
The C language was first developed in 1972 by Danny Ritchie at AT & T Bell Labs. C is high level programming language and the most popular general purpose programming language. C is written as a series of functions that call each other for processing. Even the body of the program is a function named main. The function is very flexible, allowing programmers to choose from the standard library that comes with the compiler to use third party functions from other C suppliers or to develop their own.
High level programming languages are closer to our human language as in figure 1.1 below.
The Human Language Example: English C language is high level programming. The High-Level Programming Example: Progamming C printf (C Language); printf ( High-Level); printf ( Programming );
QF 002/1/4
The program is easy to read Readability. The program is easy to maintain Maintainability The program is easy to port around on different computer platforms
Portability.
Figure 1.2: A Example of Simple C Programming The Output of the program is shown below.
QF 002/1/5
1.2
An object-oriented version of C created by Bjarne Stroudtrup. C++ has become popular because it com bines traditional C Programming with OOP capability. Most C++ compilers doesn't care what extension you give to your source code, but if you don't specify otherwise, many will use .CPP by default.
Figure 1.4 shows a simple C++ Program. Lets have a look at the first C++ program.
// Welcome To Simple C++ Programming #include <iostream..h> main() { cout << I am the best, I am the best, I am the best<< \n; cout<< I never give up, never, never, never << \n; cout<< I am a successful person; return 0; }
Figure 1.4: An example of simple C Programming The Output of the program is shown below.
QF 002/1/6
Activity 1a
Test your comprehension before continuing the next input. Check your answers on the next page. 1.1 Explain what is C programming? 1.2 Write simple sentences using C programming to display the output as given below:i.
/ / / G I \ YYY \ \ \ O O D U I MM / \ / \ / \ / \ /
ii.
PPPPPPPPPPPPP PPPPPPPPPPPPPP PPPP PPPP PPPP PPPP PPPP PPPP PPPPPPPPPPPPPP PPPPPPPPPPPPP PPPP PPPP PPPP PPPP PPPP
iii.
HH HH HH HH HHHHHH HH HH HH HH
A AAA AA AA AAAAAA AA AA
QF 002/1/7
1.3
Write simple sentences using C++ programming to display the output as given below:-
i.
/ / / G I \
YYY \ \ \ O O D U I MM / \ / \ / \ / \ /
ii.
PPPPPPPPPPPPP PPPPPPPPPPPPPP PPPP PPPP PPPP PPPP PPPP PPPP PPPPPPPPPPPPPP PPPPPPPPPPPPP PPPP PPPP PPPP PPPP PPPP
iii.
HH HH HH HH HHHHHH HH HH HH HH
A AAA AA AA AAAAAA AA AA
QF 002/1/8
Feedback 1a
Make sure you have tried to answer all the questions given.you can check your answers with the answers below. 1.1 C is the most popular general purpose and a High-Level programming language. C language is closer to our human language.
1.2 i. #include <stdio.h> main () { YYY printf ( / \ printf ( / \ printf ( / \ printf ( G O O D printf ( I U I printf ( \ MM / printf ( \ / printf ( \ / printf ( \ / printf ( \ / printf ( return 0; }
\n); \n); \n); \n); \n); \n); \n); \n); \n); \n); \n);
QF 002/1/9
ii. #include <stdio.h> main () { printf ( PPPPPPPPPPPPP printf ( PPPPPPPPPPPPPP printf ( PPPP PPPP printf ( PPPP PPPP printf ( PPPP P PPP printf ( PPPPPPPPPPPPPP printf ( PPPPPPPPPPPPP printf ( PPPP printf ( PPPP printf ( PPPP printf ( PPPP PPPP printf ( return 0; } iii. #include <stdio.h> main () { printf ( HH HH printf ( HH HH printf ( HHHHHH printf ( HH HH printf ( HH HH return 0; }
\n); \n); \n); \n); \n); \n); \n); \n); \n); \n); \n); \n);
A AAA AA AA AAAAAA AA AA
QF 002/1/10
1.3 i. #include <iostream..h> main () { YYY cout << / \ cout << / \ cout << / \ cout << G O O D cout << I U I cout << \ MM / cout << \ / cout << \ / cout << \ / cout << \ / cout << return 0; }
\n; \n; \n; \n; \n; \n; \n; \n; \n; \n; \n;
QF 002/1/11
ii. #include <iostream.h> main () { PPPPPPPPPPPPP cout << PPPPPPPPPPPPPP cout << PPPP PPPP cout << PPPP PPPP cout << PPPP PPPP cout << PPPPPPPPPPPPPP cout << PPPPPPPPPPPPP cout << PPPP cout << PPPP cout << PPPP cout << PPPP cout << PPPP cout << return 0; } iii. #include <iostream.h> main () { cout << HH HH cout << HH HH cout << HHHHHH cout << HH HH cout << HH HH return 0; }
\n; \n; \n; \n; \n; \n; \n; \n; \n; \n; \n; \n;
A AAA AA AA AAAAAA AA AA
QF 002/1/12
INPUT
A
1.3
1.4
Evolution Of C++
C programming was developed in the early 1970s at Bell Labs by Brian Kernighan and Dennis Ritchie. C compiler for each computer instead allowed the C source code to be moved easily from machine to machine, because programs written with it could be easily ported to other computer equipped with a C compiler.
QF 002/1/13
C Programming language was derived from a language call B, same programmers wondered whether the successor to C would be called D as it turns out it was called C++ (pronounced C plus plus). C++ was developed by Bjarne Stroustrup at Bell Labs in the early 1980s.
In C++, + + is an operator that increments a variable by 1. For example, to increase the value of a variable call Poli, the command would be Poli++, therefore, the C language is incre mented to its next level with C++. C++ brought object orientation to C.
note
Compiler to translate a program written in a high-level programming language into machine language.
QF 002/1/14
1.5
The structured program is viewed as a series of tasks to be done, such as reading data, processing data and generating report or output. The main program fragmented into smaller functions which are then arranged in a hierarchical structure.
Main Program
Function_a1
Function_a2
Function_b1
Function_c1
Function_b1
note
Hierarchical structure is one to many relationship; each item having one or more items below it
QF 002/1/15
i)
Object oriented Program approach and treats data is a critical element in the development of a system. It does not allow data to move freely through the system. Instead, it binds the data to the functions that are needed to manipulate them. This is to protect the data from accidental modifications by external functions to an object. In Objectoriented Programming (OOP ), problems are decomposed into a
number of entities called objects. All the data are to build function around these objects.
QF 002/1/16
Object A
Object B
Data
Data
Functions
Functions
Object C
Data
Functions
QF 002/1/17
i)
Emphasis is on data rather than functions. A program consists of a set of objects Each object has its own data
An objects data is hidden from external functions. Functions that operate on objects are tied to each data The objects communicate with one another through functions. New data and functions can be added easily whenever necessary.
Follows bottom-up approach Create a source code file with a .CPP extension Compile the source code into a file with the .OBJ extension Link your .OBJ file with any needed libraries to produce an executable program
QF 002/1/18
ii)
Class
A class is a group of objects that have similar properties (data and behaviors). An object is an instance of a class. After user created the objects, user can insert, modify or delete the objects
Object
Abstraction Inheritance
The ability of one class of objects to inherit properties from a higher class.
Abstraction is one of the basic principles of objectoriented design which allows for creating userdefined data types known as objects.
Polymorphis m
The ability of a generalized request (message) to produce different results based on the object that it is sent to. Making the data and processing within the object private, which allow the internal implementation of the object to be modified without requiring any change to the application that uses it.
Encapsulation m
QF 002/1/19
Activity 1b
Test your comprehension before continuing the next input. Check your answers on the next page.
1.4 List the differences between C and C++ in the blanks provided below.
C++
1.5
QF 002/1/20
Feedback 1b
Make sure you have tried to answer all the questions given.You can check your answers with the answers below. 1.4
Items C C++
1.5 i. ii. iii. iv. v. vi. It defragment a large progra m into smaller functions. It employs top-down approach Most of the functions share global data. The functions pass the data around the system openly. Its emphasis is on algorithms Functions transform the data from one form to another.
QF 002/1/21
Key Facts
A program is a collection of instructions that tell the computer what to do. C and C++ programming are the high level programming language that closer to human language. The Advantages of the High-level programming languages are readability, maintainability and p ortability. OOP it is an evolutionary form of modular programming which has more formal rules that allow pieces of software to be reused and interchanged between programs The difference between C and C++ are as in shown below:
QF 002/1/22
Self-Assessment
You are approaching success, please answer the questions below. If you have any problems, please discuss it with your lecturer. Wish you good luck and all the best.
Question
11
a. Write simple sentences using C programming to display the output as given below:i. ii. iii. iv. A program is a collection of instructions An object-oriented version of C created by Bjarne Stroudtrup Pokok Apakah Yang Berani? Apa Yang Anda Jumpa Di Tengah Malam?
b. Write simple sentences using C++ programming to display the output as given below:i. ii. iii. iv. A program is a collection of instructions An object-oriented version of C created by Bjarne Stroudtrup Pokok Apakah Yang Berani? Apa Yang Anda Jumpa Di Tengah Malam?
Question
12
a. Give the main characteristics of object oriented program. b. List the differences between structured programming and object oriented programming.
QF 002/1/23
Feedback On Self-Assessment
Make sure you have tried to answer all the questions given. You can check your answers with the answers below.
Answers
11
a.
i. A program is a collection of instructions
#include <stdio.h> main() {printf ( A program is a collection of instructions \n); return 0;}
QF 002/1/24
b.
i. A program is a collection of instructions
ii.
#include <iostream.h> main() {cout<< An object-oriented version of C created by Bjarne Stroudtrup; return 0;}
iii.
#include <iostream.h> main() {cout<< Pokok Apakah Yang Berani? ; return 0;}
iv.
#include <iostream.h> main() {cout<< Apa Yang Anda Jumpa Di Tengah Malam? ; return 0;}
QF 002/1/25
Answers
12
a. i. ii. iii. iv. v. vi. vii. viii. Emphasis is on data rather than functions. A program consists of a set of objects Each object has its own data and functions. An objects data is hidden from external functions. Functions that operate on ob jects are tied to each data structure. Objects communicate with one another through functions. New data and functions can be added easily whenever necessary. Follows bottom-up approach
b.
List the differences between structured programming and object oriented programming.
Structured programming Object Oriented Programming
Top-down approach Functions share global data. Emphasis is on functions Emphasis is on algorithms