Dr. Muchelule
PROGRAMMING WITH C++ NOTES
TABLE OF CONTENTS
TABLE OF CONTENTS I
History of C++ 1
INTRODUCTION 2
FUNDAMENTALS OF C++ 2
BASIC DATA TYPES 4
USER DEFINED DATA TYPE 4
Type Def 4
Enumerations 5
WRITING A PROGRAM IN C++ 6
General structure of a c++ program 6
KEYBOARD AND SCREEN I/O 7
C++ standard functions 8
Manipulator functions 9
OPERATORS 10
Arithmetic operators 10
Relational Operators 11
Logical operators 11
SPECIAL OPERATORS 12
Operator precedence 15
CONTROL STRUCTURE 16
CONDITIONAL STATEMENT 17
ITERATION/LOOP STATEMENT 20
Breaking control statement 22
FUNCTIONS 24
Defining a function 24
Actual &Formal Arguments 26
LOCAL AND GLOBAL VARIABLES 26
STORAGE CLASS SPECIFIERS 27
RECURSIVE FUNCTION 28
ARRAYS 31
Array declaration 31
Array initialization 31
PROCESSING WITH THE ARRAY 32
ARRAYS AND FUNCTIONS 33
MULTIDIMENSIONAL ARRAYS 33
Initialization of multidimensional 33
CHARACTER ARRAY 34
STRUCTURE 35
Structure Declaration/Definition 36
The structure tag 38
Initialization of a structure 39
Arrays of structures 40
ARRAYS WITHIN A STRUCTURE 40
DATA FILE OPERATIONS 41
Opening a File: 41
Closing a File 43
STREAM STATE MEMBER FUNCTIONS. 43
BINARY FILE OPERATIONS 48
STRUCTURES AND FILE OPERATIONS 50
SAMPLE PROGRAMS 53
INPUT OUTPUT 53
MANIPULATOR FUNCTIONS 54
INBUILT FUNCTION 55
SELECTION 57
ITERATION 60
CONDITIONAL BRANCHING 62
FUNCTIONS 64
ARRAYS 69
STRING 74
STRUCTURES 76
FILE PROCESSING 78
TEXT FILES 78
SEQUENTIAL FILES 82
BINARY FILES 85
History of C++
During the 60s, while computers were still in an early stage of development, many new programming languages appeared. Among them, ALGOL 60, was developed as an alternative to FORTRAN but taking from it some concepts of structured programming which would later inspire most procedural languages, such as CPL and its succesors (like C++). ALGOL 68 also directly influenced the development of data types in C. Nevertheless ALGOL was an non-specific language and its abstraction made it impractical to solve most commercial tasks.
In 1963 the CPL (Combined Programming language) appeared with the idea of being more specific for concrete programming tasks of that time than ALGOL or FORTRAN. Nevertheless this same specificity made it a big language and, therefore, difficult to learn and implement.
In 1967, Martin Richards developed the BCPL (Basic Combined Programming Language), that signified a simplification of CPL but kept most important features the language offered. Although it too was an abstract and somewhat large language.
In 1970, Ken Thompson, immersed in the development of UNIX at Bell Labs, created the B language. It was a port of BCPL for a specific machine and system (DEC PDP-7 and UNIX), and was adapted to his particular taste and necessities. The final result was an even greater simplification of CPL, although dependent on the system. It had great limitations, like it did not compile to executable code but threaded-code, which generates slower code in execution, and therefore was inadequate for the development of an operating system. Therefore, from 1971, Denis Ritchie, from the Bell Labs team, began the development of a B compiler which, among other things, was able to generate executable code directly. This "New B", finally called C, introduced in addition, some other new concepts to the language like data types (char).
In 1973, Denis Ritchie, had developed the basis of C. The inclusion of types, its handling, as well as the improvement of arrays and pointers, along with the later demonstrated capacity of portability without becoming a high-level language, contributed to the expansion of the C language. It was established with the book "The C Programming Language" by Brian Kernighan and Denis Ritchie, known as the White Book, and that served as de facto standard until the publication of formal ANSI standard (ANSI X3J11 committee) in 1989.
In 1980, Bjarne Stroustrup, from Bell labs, began the development of the C++ language, that would receive formally this name at the end of 1983, when its first manual was going to be published. In October 1985, the first commercial release of the language appeared as well as the first edition of the book "The C++ Programming Language" by Bjarne Stroustrup.
During the 80s, the C++ language was being refined until it became a language with its own personality. All that with very few losses of compatibility with the code with C, and without resigning to its most important characteristics. In fact, the ANSI standard for the C language published in 1989 took good part of the contributions of C++ to structured programming.
From 1990 on, ANSI committee X3J16 began the development of a specific standard for C++. In the period elapsed until the publication of the standard in 1998, C++ lived a great expansion in its use and today is the preferred language to develop professional applications on all platforms.
C++ has been evolving, and a new version of the standard, c++09, is being developed to be published before the end of 2009, with several new features
INTRODUCTION
C++ is a structured programming language with object oriented programming methodology, in which the software reusability, testability, maintainability, portability and reliability are the key features and requisites of modern software development
C++ has become popular due to the following reasons:
a) It supports all features of both structured and object oriented programming
b) It gives the easiest way to handle the data hiding and encapsulation with the help of powerful keywords such has class, private, public and protected
c) Inheritance, one of the most powerful design concepts is supported with single inheritance and multiple inheritance of the base class and derived classes.
d) Polymorphism thro virtual functions, virtual base classes and virtual destructors give the late binding of the computer.
e) It provides overloading of operators and functions
f) C++ focuses on functions and class templates for handling parameterized data types
g) Exceptions handling is done by extra keywords namely try, catch, and throw
h) Provides friends, static, methods, contractors and destructors for the class objects
FUNDAMENTALS OF C++
They are smallest individual units in a program ,they are divided into the following
Keywords
Identifiers
Constants
String
operators
Identifiers
Identifiers refer to t he names of variables, functions, array, classes created by the user(programmer) .
they can be defined as the name of the variable and some other program elements using the combination of the following characters
Alphabets :a ……..z ,A…..Z
Numerals :0…..9
Underscore : _
In object oriented programming, upper case and lower case are distinct , a variable should not begin with a digit.
Rules for naming a identifiers
1. only alphabetic characters, digits and underscore are permitted
2. the name cannot start by a digit
3. upper case and lower case are distinct
4. a declared keyword cannot be used as variable name
Keywords
Key words are also identifiers but cannot be user defined since they are used by the language ,the following words are reserved for use as key words
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while
Constants
These are the values which are set and will remain unchanged through out the program
Types of constants
String constant
Numeric constants
Character constant
String constant: is a sequence of alphanumeric characters enclosed in double quotation marks whose maximum length is 255 characters
Examples
“result” “ksh 3000” “my program’
Numeric constants: there are four types of numeric constants: integer constant, floating point constant, hex constant, octal constant
Declaration
Integer constant integer constant do not contain decimal points, the variable can be declared as integer in the following ways
int x,y;
short int x,y;
long int x,y
integer data: int stands for the integer data
short integer : used to declare the short integer data whose maximum size is between -32,768 to 32,767
long integer : used to declare the short integer data whose maximum size is between -2,147,483,648 to 2,147,483,647
BASIC DATA TYPES
|Name |Description |Size* |Range* |
|char |Character or small integer. |1byte |signed: -128 to 127 |
| | | |unsigned: 0 to 255 |
|short int (short) |Short Integer. |2bytes |signed: -32768 to 32767 |
| | | |unsigned: 0 to 65535 |
|int |Integer. |4bytes |signed: -2147483648 to 2147483647 |
| | | |unsigned: 0 to 4294967295 |
|long int (long) |Long integer. |4bytes |signed: -2147483648 to 2147483647 |
| | | |unsigned: 0 to 4294967295 |
|bool |Boolean value. It can take one of two values: true or |1byte |true or false |
| |false. | | |
|float |Floating point number. |4bytes |3.4e +/- 38 (7 digits) |
|double |Double precision floating point number. |8bytes |1.7e +/- 308 (15 digits) |
|long double |Long double precision floating point number. |8bytes |1.7e +/- 308 (15 digits) |
|wchar_t |Wide character. |2 or 4 bytes |1 wide character |
USER DEFINED DATA TYPE
Type Def
Is used to define new data items that equivalent to the existing data types once a user defined data is declared then variables arrays, structures and so on can be declared in terms of new data types.
Typedef data type new type
Type def is a keyword for declaring the new items and data type is an existing data type being converted to the new name. For example
typedef int integer;
typedef float real;
integer i , j , ;
real a,b;
The type def is used in a program to make readable and portable.
Enumerations
An enumeration data type is a set of values represented by identifiers called enumeration constants. The enumeration constants are specified when type is defined:
The general format is
Enum user-defined-name
Member 1;
Member 2;
Member n;
When enum is a keyword for defining the enumeration data type and the braces are essential. The members of the enumeration data type such as member 1, member 2, and member n. Once the enumeration data type is defined, it can be declared in the following ways; storage –class enum, user defined –name
Variable 1, variable 2 , variable n where the storage class is optional
Example:
1) enum days { mon , tue , wed , thu , fri , sat , sun , }
enum days day1, day2 , day3, day4
2) enum drinks
cola , meza, limca, ranson
enum drinks rav, raju, rani
3) enum genus
tennis, chess, shuttle, swimming, walking;
enum games student, staff;
The enum constants can be assigned to the variable like
day1=mon;
day2=tue
“Enumeration
include
void main (void)
enum days {mon, tue, wed, thu, fri, sat, sun }
day 1, day2, day3, day4, day5, day6, day7 ;
day1=mon;
day1=mon;
day2=tue;
day3=wed;
day4=thu;
day5=fri;
day6=sat;
day7=sun;
Count < < “ Monday = “ < < day 1 < < endl.
Count < < “ Tuesday = “ < < day2 < < endl.;
Count < < “ Wednesday = “ < < day3 < < endl.;
Count < < “ Thursday = “ < < day4 < < endl.;
Count < < “ Friday = “ < < day5 < < endl.;
Count < < “ Saturday = “ < < day6 < < endl.;
Count < < “ Sunday = “ < < day7 < < endl.;
Output Monday= 0----------------Sunday = 6
These integers are normally chose automatically but they can also be specified by the programmer.
Enum sample {mon, tue, wed, = lo , thu, fri, sat =ls sun}
Day1, day2, day3, day4, day5, day6, day7, ;
The c++ compile assigns the enum constants as
Monday = 0
Tuesday = 2
Wednesday =10
Thursday = 11
Friday =12
Saturday =15
Sunday =16
WRITING A PROGRAM IN C++
General structure of a c++ program
[pic]
(i) Declaration of variables
C++ allows declaration of the variables before and after executable statements. A declaration is a process of naming of the variables their corresponding data types in C++. Usually a declaration part is placed after the beginning statement and some times, some of the global variable can be defined or declared outside the program, a variable is an object that may take on values the specified type.
The general format is:
Data _type identifiers, id1, id2…idn;
Examples
Char ch;
Short int x, y;
Unsigned limit;
(i) Statements
Is a computer program that carries out some actions.
a) Expression statement
Consist of valid C++ expression followed by a semicolon.
e.g x=y;
Sum=x+y;
b) Compound statement
a group of valid C++ expressions plaud within a {and} statement is called compound statement.
e.g {
a=b+c;
x=x*x;
y=a+x;
}
c) Control statement
The control statement is used for the program flow and to check the conditions of the given expression or a variable or a constant. They are used with control structures.
The skeleton of a typical C++ program structure is given below:
Program heading
Begin {
Type or variables declaration
Statement of operation
Results
End }
KEYBOARD AND SCREEN I/O
a) cout
The cout is used to display an object onto the standard device, normally the video screen. The insertion operator ( ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.