You are on page 1of 25

Introduction To C++ Programming Language

QF 002/1/1

UNIT 1

INTRODUCTION TO C++ PROGRAMMING LANGUAGE

OBJECTIVES

General Objective

To understand and recognize the fundamental concept of C and C++ Programming.

Specific Objectives

At the end of the unit you should be able to:-

 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

Introduction To C++ Programming Language

QF 002/1/2

INPUT

1.0

Introduction

Sir.. What is a program?

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

Machine language - the native language of the computer

Introduction To C++ Programming Language

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 );

The Machine Language Example: Binary code 10001110010001000 10000010000111000 11100100111001111

Figure 1.1: An Example of the Language Spectrum

Introduction To C++ Programming Language

QF 002/1/4

The Advantages of the High-level programming languages are:

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.

Here is a simple C program example. Lets have a look.


/* Welcome To Simple C Programming */ #include <stdio.h> main() { printf ( I am the best, I am the best, I am the best\n); printf ( I never give up,never,never,never\n); printf ( I am a successful person); return 0; }

Figure 1.2: A Example of Simple C Programming The Output of the program is shown below.

Figure 1.3: Output from the sample of C Programming

Introduction To C++ Programming Language

QF 002/1/5

1.2

Introduction To C++ Programming

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.

Figure 1.5: Output from the sample of C++ Programming


note 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

Introduction To C++ Programming Language

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

III III III III III

Introduction To C++ Programming Language

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

III III III III III

Introduction To C++ Programming Language

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);

Introduction To C++ Programming Language

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

III III III III III

\n); \n); \n); \n); \n);

Introduction To C++ Programming Language

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;

Introduction To C++ Programming Language

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

III III III III III

\n; \n; \n; \n; \n;

Introduction To C++ Programming Language

QF 002/1/12

INPUT
A

1.3

Comparison Between C and C++

The difference between C and C++ are given below:

Items Header file Input function Output Function

C #include <stdio.h> scanf() printf()

C++ #include<iostream.h> cin>> Cout<<

Table 1.1: Differences Between C And C++

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.

Sources: Peter Nortons (1997) Introduction To Computer

Introduction To C++ Programming Language

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.

Sources: Peter Nortons (1997) Introduction To Computer

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.

Introduction To C++ Programming Language

QF 002/1/14

1.5

Differences Between Structured Programming And Object Oriented Programming

1.5.1 Structured Program

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

Figure 1.6: Typical structure of a Structured Program

note

Hierarchical structure is one to many relationship; each item having one or more items below it

Introduction To C++ Programming Language

QF 002/1/15

i)

The main characteristics of structured program

It decomposes a large program 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 emphasizes on algorithms

The functions transform the data from one form to another.

1.5.2 Object Oriented Program (OOP)

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.

Introduction To C++ Programming Language

QF 002/1/16

Object A

Object B

Data

Data

Functions

Functions

Object C

Data

Functions

Figure 1.7: Structure of Object Oriented Programming

Introduction To C++ Programming Language

QF 002/1/17

i)

The main characteristics of OOP

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

Introduction To C++ Programming Language

QF 002/1/18

ii)

Basic OOP concepts

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

Communicating between objects, similar to a function call in traditional programming.


Message

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

Introduction To C++ Programming Language

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.

Items Header file Input function Output Function Comment

C++

1.5

Give the main characteristics of structured program.

Introduction To C++ Programming Language

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++

Header file Input function Output Function

#include <stdio.h> scanf() printf()

#include<iostream.h> cin>> Cout<<

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.

Introduction To C++ Programming Language

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:

Items Header file Input function Output Function

C #include <stdio.h> scanf() printf()

C++ #include<iostream.h> cin>> Cout<<

Table 1.2: Differences Between C And C++

Introduction To C++ Programming Language

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.

Introduction To C++ Programming Language

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;}

ii. An object-oriented version of C created by Bjarne Stroud trup


#include <stdio.h> main() {printf ( An object-oriented version of C crea ted by Bjarne Stroudtrup \n); return 0;}

iii. Pokok Apakah Yang Berani?


#include <stdio.h> main() {printf ( sebut Pokok Apakah Yang Berani?? \n); return 0;}

iv. Apa Yang Anda Jumpa Di Tengah Malam?


#include <stdio.h> main() {printf ( Apa Yang Anda Jumpa Di Tengah Malam? \n); return 0;}

Introduction To C++ Programming Language

QF 002/1/24

b.
i. A program is a collection of instructions

#include <iostream.h> main() {cout<< A program is a collection of instructions ; return 0;}

ii.

An object-oriented version of C created by Bjarne Stroudtrup

#include <iostream.h> main() {cout<< An object-oriented version of C created by Bjarne Stroudtrup; return 0;}

iii.

Pokok Apakah Yang Berani?

#include <iostream.h> main() {cout<< Pokok Apakah Yang Berani? ; return 0;}

iv.

Apa Yang Anda Jumpa Di Tengah Malam?

#include <iostream.h> main() {cout<< Apa Yang Anda Jumpa Di Tengah Malam? ; return 0;}

Introduction To C++ Programming Language

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

Bottom-up approach Consist of a set of objects Emphasis is on data Emphasis is on objects

CONGRATULATIONS May success be with you always..

You might also like