You are on page 1of 26

1 MOHAMMED FEROZ

INTRODUCTION TO C++ PROGRAMMING

CHAPTER-1

2 MOHAMMED FEROZ

Introduction C++ is a object oriented programming language (OOP). It is about objects which contain data and functions to manipulate that data. A single unit that contains data and functions that operate on the data is called an object. It is superset of C language which consists the Features present in C are also present in C++.

C++ = c + objects +
Why use of C++ ? Question: Explain the uses of C++ language.? simple Maintainability Portability Data Hinding Error Handling Suitable for large applications. Concept of object oriented programming (OOP) : Question : Explain the concepts of oop? Objects classes Data Hiding Abstraction Encapsulation Inheritance Polymorphism.
3 MOHAMMED FEROZ

1. Object : Object is an real world entity which consits of both data and functions. It is also an instance of class. Example : person, table and car. 2. Class : Define common characteristics of similar objects. classes are used to create objects known as instance.

4 MOHAMMED FEROZ

3. Data Hiding. Data Hiding allows to hide the information of the object from the user. 4. Data Abstraction : Data Abstraction is a programming technique which separates the interface from the implementation. 5. Data encapsulation : Combining the data and function into a single unit is called data encapsulation. It is done by using the concept of class. Example : class Vehicle { attributes+methods } 6. Inheritance : Deriving a new class from an existing class is known as inheritance. 7. Polymorphism : It enables an object to exists in many forms of various instances(objects) of other classes. The Compilation Process of C++ : A program goes from text files(or source files)to processor instructions as follows: Source File Compiler Object File Linker Excutable file MemoryOS output

5 MOHAMMED FEROZ

Question : What is token in CPP ? Token : The smallest meaningful symbols in the language is called Token. It has special meaning known to the compiler.

Question : Explain various data types supported by CPP Data Types : Data types are used to define tha data hold by the variables(data).

6 MOHAMMED FEROZ

Question : What is variable in CPP? Variable : Variable are the quantity which can be changed during the execution (running) of program. Example : int x,a; float p=23.56; bool b=false; char ch= h; Where x ,a, p,b, and ch are variable names. Question: Explain various operators are available in CPP? Operators : operations.
Sno 1 2 3 4 5

Operators are the symbols which are used to perform mathematical

Mathematical operators Symbol + * / % Description add or sum Subtract Multiplication Division Modulus Example a+b , 2+3=5 a-b , 10-6=4 a*b , 2*3=6 a/b , 6/3=2 a%b , 6%5=1

7 MOHAMMED FEROZ

Relational operators Sno 1 2 3 Name > < >= Symbol Greater than Less than greather than or equal Lessthan or equal Equal to Not equal Example 5>4 is TRUE 5<4 is TRUE 4>=4 is TRUE

4 5 6

<= == != Logical operators X TRUE FALSE FALSE FALSE Y TRUE FALSE TRUE FALSE

3<=4 is TRUE 5= = 5 is TRUE 5!=4 is TRUE

X && Y - logical AND TRUE FALSE FALSE FALSE

X || Y

- logical OR TRUE TRUE TRUE FALSE

Assignment Operator : The symbol = is known as assignment operator used to assign the value to the variable Example : int a = 5; char ch=c;

Escape Sequence : It is a special symbol to format the output information

8 MOHAMMED FEROZ

EXAMPLE PROGRAMS 1. Write a CPP program to display simple message

Explanation : Line 1 : Represents comments ( // ) Line 2: consits # called preprocessor command, #include include the file streamio.h for input/output ( example : cout and cin function) Line 3: void main() {...} represents main() function where the program starts first. Line 4 and 8: are called curly braces within main() function to group the statements as a block. Line 5: clrscr( ) function to clear previous output result. Line 6: represents a simple statement to print the Hello world on the output screen by using the output command cout. Line 7 : getch( ) main window. indicates that the program completed successfully and exit to

9 MOHAMMED FEROZ

2. Write a CPP program to add two numbers

10 MOHAMMED FEROZ

11 MOHAMMED FEROZ

CONTROL STRUCTURES
CHAPTER- 2

12 MOHAMMED FEROZ

CONTOL STRUCTURE Control structures allows to change the behaviour of the program execution. It executes some statements based on conditions and repeat the same statement into number of times and also jumps from one point to another point in a program. There are three types of control structures. 1. Conditional statements structure 2. Repetational (loop) statements 3. Jump statements 1. Conditional Statement : There are four types of conditional statements are as follows 1.1. 1.2. 1.3. 1.4. Simple if- statement if -else statement Nested if-else Switch statement

Simple if- statement : It execute the statement if the condition is true. Syntax: if(condition) { Block of Statement }

13 MOHAMMED FEROZ

Example : void main() { int a; cout<<enter the value of A: ; cin>>a; if(a= =100) { cout<< \n Welcome to KKU ; } getch(); } 1.2 if-else statement : It execute the statement x if the condition is true otherwise statement y. Syntax: if(condition) { Statement-x } else { Statement-y }

Example : void main() { int a=4,b=7 if(a>b) { cout<< \n A is the largest ; }


14 MOHAMMED FEROZ

else { cout<< \n B is the largest ; } getch(); } 1.3 nested if else statement : It checks the condition first and execute the statements. Syntax: if(condition-1) { Statement-x } else if(condition-2) { Statement-y } else { Statement-z } Example : void main() { int a=3,b=5,c=8; if(a>b &&a>c) { cout<< A is the largest; } else if( b>a && b>>c) { cout<< B is the largest; } else { cout<< C is the largest; }

1.4 switch statement : It consits a series of conditions, based on value in switch, it executes the case. Each case must end with break statement. It takes only integer, character data type only.

15 MOHAMMED FEROZ

Example : void main() { int p; cout<<enter P: \n; cin>>p; switch(p) { case 1: cout<< Friday; break; case 2: cout<< Saturday; break; case 3: cout<<Sunday; break; case 4: cout<< Monday; break; case 5:
16 MOHAMMED FEROZ

cout<< Tuesday; break; case 6: cout<< Wednesday; break; case 7: cout<< Thursday; break; } getch(); }

1. Repetitional (loop) statements : It enables to repeat some statements until condition is true and stops if the condition is false. There are three types of loop control statements 2.1 While loop statement 2.2 do-while- loop statement 2.3 for - loop statement 2.1 While loop statement : It executes the statements as long as the condition is true. If the condition fails it terminates the loop. Syntax while(condition) { Statements. }

17 MOHAMMED FEROZ

Example : void main() { int i=0; while(i<5) { cout<<\n Hello; i++; } } 2.2 do-while loop statement : It is similar to while loop except that it executes the statement and then check the condition. Syntax:

do { Statements. } while(condition);

Example : void main() { int i=0; do { cout<<\n Hello; i++; } while(i<5); }


18 MOHAMMED FEROZ

2.3 for- loop statement : The general form of for loop is as follows Syntax
for(initialization;condition;increment/decrement) { body }

Example : void main() { int i; for(i=0;i<5;i++) { cout<< \n Hello; } getch(); } 3. Jump control statement : It enables to jump from one point to another point in a program . There are two jump control statements 3.1 return : It returns the value to the calling funtion e.g : return value 3.2 break : It stop the execution of statement and terminates the loop. 3.3 continue : It stops current statement and execute the next statement.

19 MOHAMMED FEROZ

Example : void main() { int i; for(i=0;i<5;i++) { if(i==3) break; cout<< \n I =<< i; } getch(); } Output : I=0 I=1 I=2 void main() { int i; for(i=0;i<5;i++) { if(i==3) continue; cout<< \n I =<< i; } getch(); } Output : I=0 I=1 I=2 I=4

ARRAYS
Q: Explain the concept of Arrays in C++ Arrays : An Array is a collection of similar elements of same type which shares a common name. syntax: Type array-name[size]; Example : int x[5]; Where x = array-name. Size is total number of elements in an array. To access a elements, use the array name and an index, array index starts from 0 to size-1. x[0], x[1], x[2], x[3], x[4]

20 MOHAMMED FEROZ

Array Layout (Representation in Memory) : It can be represented in the memory for the following declaration Int x[5] = {20,40,35,60,10} 0 1 2 3 4

X X[0]=20 X[1]=40 X[2]=35 X[3]=60 X[4]=10

1. Write a CPP Program to display Array elements void main() { int i; int a[5]={5,10,15,20,25}; clrscr(); for(i=0;i<5;i++) cout<<"\n A["<<i<<"]="<<a[i]; getch(); } A[0] = 5 A[1] = 10 A[2] = 15 A[3] = 20 A[4] = 25

2. Write a CPP program that reads array of numbers from the user

21 MOHAMMED FEROZ

Function
Q: What is function in C++ explain with an example? Function: In C++ , a function is a block ( group) of statement used to perfom some specific kind of task. syntax return-type function-name( argument_list) { Block of statements }

22 MOHAMMED FEROZ

Example void main() { cout<< Example for function; print(); getch(); } void print() { cout<< \n in function } Q: Explain pass by value with an example Pass by value : Passing a copy of variable to the called funtion from the calling function. Any change in the called function will not reflect to the original values. void main() { int a=5, b=4; cout<< Before passing <<\n; cout<<A =<<a <<\n B =<<b; swap(a,b); cout<< \n After passing <<\n; cout<<A =<<a <<\n B =<<b; getch(); } void swap(int a, int b) { a = a+5; b = b+4; }

23 MOHAMMED FEROZ

Q: What are the access specifiers in C++ Access Specifiers : It is used to specify the accessibility of a program class alpha { private://visible inside this class only protected://visible inside this class //and its immediate derived class public://visible to all };

Class and object


Q: Explain class and object with an example program. Class : Class is a way to bind the data and procedures that operates on data. Class declaration: class class_name { private: variable declarations;//class function declarations;//members public: variable declarations;//class function declarations;//members }; Object: Objects are run time instance of a class.

Object declaration: class_name obj1; class_name obj2,obj3;


24 MOHAMMED FEROZ

INHERITANCE Q: Explain inheritance and its types Creating a new class from an existing class is known as Inheritance. The new class is called derived class and existence class is known as base class. Types of Inheritance

25 MOHAMMED FEROZ

Q: Define the following terms used in C++


a) Constructor b) Destrcutor c)Operator Overloading d)friend function e) Pointer

1. Constructor: A member function which construt the object and it has same name as class name is known as constructor. 2. Destructor : A member function that destroys the object. 3. Operator Overloading : It has the ability to provide the operators with a special meaning for a data type. 4. Friend function: It is used in the situation where two classes want to share a common function. 5. Pointer: A pointer is a varibale that stores the address of another variable

26 MOHAMMED FEROZ