You are on page 1of 44

Programing using C++

By: Girish Kumar N G Assistant Professor Bangalore Institute of Technology V.V.Puram, K.R.Road, Bangalore-560004

Syllabus
UNIT - 1 C++, AN OVERVIEW: Getting started, the C++ program, Preprocessor Directives, The Built-In Array Data Type, Dynamic Memory Allocation and Pointers, An Object based Design, An Object-Oriented Design, An Exception based Design. 6 Hours UNIT - 2 THE BASIC LANGUAGE: Literal Constant, Variables, Pointer Type, String Types, const Qualifier, Reference Types, the bool type, Enumeration types, Array types. The vector container type. 6 Hours

UNIT - 3 OPERATORS: Arithmetic Operators, Equality, Relational and Logical operators, Assignment operators, Increment and Decrement operator, The conditional Operator, Bitwise operator, bit-set operations. Statements: if, switch, for Loop, while, break, goto, continue statements. 7 Hours UNIT - 4 FUNCTIONS: Prototype, Argument passing, Recursion and linear function. 6 Hours UNIT - 5 EXCEPTION HANDLING: Throwing an Exception, Catching an exception, Exception Specification and Exceptions and Design Issues. 6 Hours

UNIT - 6 CLASSES: Definition, Class Objects, Class Initialization, Class constructor, The class destructor, Class Object Arrays and Vectors. 7 Hours UNIT - 7 Overload Operators, Operators ++ and --, Operators new and delete. 7 Hours UNIT 8 Multiple Inheritance, public, private & protected inheritance, Class scope under Inheritance. 6 Hours

Textbooks
Prescribed: C++ Primer S. B. Lippmann & J. Lajoie, 3rd Edition, Addison Wesley, 2000. Advice: Let us C++ by Yeshwanth Kanitkar and Programming using C++ by Balaguru Swamy

Topics
1.1 Why Program? 1.2 Computer Systems: Hardware and Software 1.3 Programs and Programming Languages 1.4 What Is a Program Made of? 1.5 Input, Processing, and Output 1.6 The Programming Process

1-6

1.1 Why Program?


Computer programmable machine designed to
follow instructions

Program instructions in computer memory to make


it do something

Programmer person who writes instructions


(programs) to make computer perform a task SO, without programmers, no programs; without programs, the computer cannot do anything

1-7

1.2 Computer Systems: Hardware and Software


Main Hardware Component Categories
1. 2. 3. 4. 5. Central Processing Unit (CPU) Main memory (RAM) Secondary storage devices Input Devices Output Devices

1-8

Main Hardware Component Categories

1-9

Central Processing Unit (CPU)


Includes Control Unit
Retrieves and decodes program instructions Coordinates computer operations

Arithmetic & Logic Unit (ALU)


Performs mathematical operations

1-10

The CPU's Role in Running a Program


Cycle through: Fetch: get the next program instruction from main memory Decode: interpret generate a signal the instruction and

Execute: route the signal to the appropriate component to perform an operation

1-11

Main Memory
Holds both program instructions and data

Volatile erased when program terminates or computer is turned off


Also called Random Access Memory (RAM)

1-12

Main Memory Organization


Bit
Smallest piece of memory Stands for binary digit Has values 0 (off) or 1 (on)
8 bits 0 1 1 0 0 1 1 1

Byte
Is 8 consecutive bits

Word
Usually 4 consecutive bytes Has an address
1-13

1 byte

Secondary Storage
Non-volatile - data retained when program is not running or computer is turned off Comes in a variety of media
magnetic: floppy or hard disk drive, internal or external optical: CD or DVD drive flash: USB flash drive
1-14

Input Devices
Used to send information to the computer from outside
Many devices can provide input
keyboard, mouse, microphone, scanner, digital camera, disk drive, CD/DVD drive, USB flash drive

1-15

Output Devices

Used to send information from the computer to the outside Many devices can be used for output
Computer screen, printer, speakers, disk drive, CD/DVD recorder, USB flash drive

1-16

Software Programs That Run on a Computer


Operating system software
programs that manage the computer hardware and the programs that run on the computer how many programs can run at once?
Single tasking - one program at a time (MS-DOS) Multitasking multiple programs at a time (UNIX, Windows XP/Vista/7) Single user MS-DOS, early versions of Windows Multiuser - UNIX

how many people can use computer at the same time?

Application software
1-17

programs that provide services to the user.


Ex: word processing, games, programs to solve specific problems

Program

1.3 Programs and Programming Languages


a set of instructions directing a computer to perform a task

Programming Language
a language used to write programs

1-18

Programs and Programming Languages


Types of languages
Low-level: used for communication with computer hardware directly.
High-level: closer to human language

1-19

From a High-level Program to an Executable File


a) Create file containing the program with a text editor. b) Run preprocessor to convert source file directives to source code program statements. c) Run compiler to convert source program statements into machine instructions.
1-20

From a High-level Program to an Executable File


d) Run linker to connect hardware-specific library code to machine instructions, producing an executable file. Steps b) through d) are often performed by a single command or button click.
Errors occuring at any step will prevent execution of the following steps.
1-21

From a High-level Program to an Executable File

1-22

1.4 What Is a Program Made Of?


Common elements in programming languages:
Key Words Programmer-Defined Identifiers Operators Punctuation Syntax

1-23

Example Program
#include <iostream> using namespace std; int main() { double num1 = 5, num2, sum; num2 = 12;

sum = num1 + num2; cout << "The sum is " << sum; return 0;
}
1-24

Key Words
Also known as reserved words

Have a special meaning in C++


Can not be used for another purpose

Written using lowercase letters


Examples in program (shown in green): using namespace std; int main()
1-25

Programmer-Defined Identifiers
Names made up by the programmer

Not part of the C++ language


Used to represent various things, such as variables (memory locations) Example in program (shown in green): double num1

1-26

Operators
Used to perform operations on data

Many types of operators


Arithmetic: +, -, *, / Assignment: =

Examples in program (shown in green): num2 = 12; sum = num1 + num2;

1-27

Punctuation
Characters that mark the end of a statement, or that separate items in a list Example in program (shown in green): double num1 = 5, num2, sum; num2 = 12;

1-28

Lines vs. Statements


In a source file,
A line is all of the characters entered before a carriage return. Blank lines improve the readability of a program. Here are four sample lines. Line 3 is blank:
double num1 = 5, num2, sum; num2 = 12; sum = num1 + num2;

1-29

Lines vs. Statements


In a source file,
A statement is an instruction to the computer to perform an action. A statement may contain keywords, operators, programmer-defined identifiers, and punctuation. A statement may fit on one line, or it may occupy multiple lines. Here is a single statement that uses two lines:
double num1 = 5, num2, sum;

1-30

Variables
A variable is a named location in computer memory (in RAM) It holds a piece of data It must be defined before it can be used Example variable definition:
- double num1;

1-31

1.5 Input, Processing, and Output


Three steps that many programs perform
1) Gather input data
from keyboard from files on disk drives

2) Process the input data 3) Display the results as output


send it to the screen or a printer write it to a file

1-32

1.6 The Programming Process


1. Define what the program is to do.

2. Visualize the program running on the computer.


3. Use design tools to create a model of the program.
Hierarchy charts, flowcharts, pseudocode, etc.

4. Check the model for logical errors.


5. Write the program source code. 6. Compile the source code.

1-33

The Programming Process (cont.)


7. Correct any errors found during compilation. 8. Link the program to create an executable file. 9. Run the program using test data for input. 10. Correct any errors found while running the program.
Repeat steps 4 - 10 as many times as necessary.

11. Validate the results of the program.


Does the program do what was defined in step 1?

1-34

What is C++?
To understand let us consider an example: i=2; j=i++; So what will be the j value? j= 2,3. So i++ can be said as post increment operation means the output first displays the initial value and then it displays the incremented value.

In a similar way here C++ is first the output takes initial value of C means all the concepts of C what you have read previously is there in C++, and then the next output which is an incremented value is compared with the new concepts that are introduced in C++ which are not present in simple C language like inheritance, polymorphism and so on

Before Getting Into C++ let us Practice Some C Programs

Check the given number is palindrome number or not using c program

#include<stdio.h> int main(){ int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num){ r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp); return 0; }

Reverse a string in c

#include<iostream.h> #include<string.h> int main(){ char str[50]; char *rev; cout<<Enter any string:; cin>>s; rev = strrev(str); cout<<"Reverse string is : %s<<rev; return 0; }

Conversion from uppercase to lower case using c program

#include<stdio.h> #include<string.h> int main(){ char str[20]; int i; printf("Enter any string->"); scanf("%s",str); printf("The string is->%s",str); for(i=0;i<=strlen(str);i++){ if(str[i]>=65&&str[i]<=90) str[i]=str[i]+32; } printf("\nThe string in lower case is->%s",str); return 0; }

C++ Program
So let us now learn what is all about a language C++ What are the applications of C++ What are advantages of using C++ over C and comparison of C++ with other languages

You might also like