Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
KGiSL
Introduction to C++
It is an extension of c.
It was developed by Bjarne Stroustrup. Combination of more than one programming language. It is mainly used for creating complicated applications.
KGiSL
Page 2
KGiSL
Page 3
Programming Paradigms
A programming paradigm is a model of programming based on distinct concepts that shapes the way programmers design, organize and write programs. - Monolithic Programming - Procedural Programming - Structured Programming
KGiSL
Page 4
Monolithic Programming
- Monolithic Programming indicates the program which contain a single function for the large program. - Monolithic programming will not divide the program and it is a single thread of execution. - In monolithic programming data variables are declared globally and the statements are written in sequence.
KGiSL
Page 5
Procedural Programming
- A procedural programming language is one where programs are organized into blocks of code called variously "subroutines", "functions", or "procedures", each of which handles one particular task. - FORTRAN and COBOL
KGiSL
Page 6
Structured Programming
- A computer programming technique that follows a top down design approach with block oriented structures. - In structured programming languages such as PASCAL and C larger programs are developed. - Programs are divided into multiple sub modules and procedures. - Each module has its own set of local variable. - User-defined data types are introduced.
KGiSL
Page 7
Object-oriented programming (OOP) is a programming paradigm that uses "objects" data structures consisting of data fields and methods together with their interactions to design applications and computer programs.
KGiSL
Page 8
There are several fundamental or key concepts in object-oriented programming. 1. 2. 3. 4. 5. 6. 7. 8. Classes Objects Method Data Abstraction Encapsulation Inheritance Polymorphism Reusability
KGiSL
Page 9
Objects
Objects are primary run-time entities in object-oriented programming. An object is a real world thing or concept expressed as a software representation. Hardware,software,documents,human beings and concepts are example of objects.
KGiSL
Page 10
Classes
A class is grouping of objects having identical properties,common behavior and shared relationship. The entire group of data and code of an object can be built as a user-defined data type using class. Objects are variables of a type class. A class is a model of the object.
KGiSL
Page 11
class A { private: data member1; data member2; data member(n); public: method1(); method2(); };
Members
KGiSL
Page 12
Class : car Properties : company,model,color and capacity. Actions : speed(),average() and break() Class : computer Properties : brand,price,monitor,hard disk Actions : processing(),display() and printing()
KGiSL
Page 13
Example
KGiSL
Page 14
Method
Method is a set of procedural statements for achieving the desired result. It performs different kinds of operations on different data types.
KGiSL
Page 15
Data Abstraction
Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.
KGiSL
Page 16
Encapsulation
The packing of data and functions into a single component is known as encapsulation. C++ supports the features of encapsulation using classes. The data is not accessible by outside functions. Datas can be accessed only the functions defined inside the class.
Inheritance
Inheritance is an important and powerful concept in object oriented technology. Inheritance is the method by which objects of one class get the properties of objects of another class. It means,existing class can be used to create a new class. New class is known as derived class and the existing class is known as base class.
KGiSL
Page 18
Inheritance
Red
Yellow
Blue
Orange
Green
Violet
KGiSL
Page 19
Polymorphism
Polymorphism allows the same function name to act differently in different classes. It is an important feature in OOP concept and has the ability to take more than one form.
KGiSL
Page 20
C
C- Procedure Oriented C does not have any classes or objects. C input/output is based on library and the processes are carried out by including functions. C functions do not support overloading. C does not support new or delete commands.
C++ supports Overloading. New keyword is used to create an object for a class.
KGiSL
Page 21
KGiSL
Page 22
Structure of a Program
// First Program #include <iostream.h> int main () { cout << Welcome to KGiSL!"; return 0; }
KGiSL
Page 23
Structure of a Program(Contd) main() - The c++ program is a collection of functions. - Every program must have a main(). Comments - C++ introduces a new comment symbol //(double slash) - Multiline comment /* line1 line2 line3 */
KGiSL
Page 24
KGiSL
Page 25
Variables,Constants and Data types in C++ Variables - A variable is the storage location in memory that is stored by its value. - A variable is identified or denoted by a variable name. - The variable name is a sequence of one or more letters, digits or underscore, for example: character _
KGiSL
Page 26
Page 27
The most commonly used Data Types in C++ programming language: - short int - int - long int - float - double - long double - char
KGiSL
Page 28
Declaration of variable
The syntax for declaring variable name is:datatype variable_name; Example: int a; If there exists more than one variable of the same type, such variables can be represented by separating variable names using comma. For instance int x,y,z ;
KGiSL
Page 29
Operators in C++
Assignment Operator denoted by = Arithmetic operators denoted by +, -, *, /, % Compound assignment Operators denoted by +=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |= Increment and Decrement operator denoted by ++, - Relational and equality operators denoted by ==, !=, >, <, >=, <= Logical operators denoted by !, &&, || Conditional operator denoted by ? Comma operator denoted by ,
KGiSL
Page 30
KGiSL
Page 31
Conditional Structure
- It is also known as Decision Making Statements. - C++ has the following statements if statement ifelse statement Else if statement
KGiSL
Page 32
if statement:
syntax: if ( condition ) statement;
KGiSL
Page 33
#include<iostream.h> #include<conio.h> void main() { int x=10,y=20; if(x<y) cout<<x is less than y; getch(); }
KGiSL
Page 34
If.else statement
Syntax:
if ( condition ) { // Execute these statements if TRUE } else { // Execute these statements if FALSE }
KGiSL
Page 35
KGiSL
Page 36
ifelse if statement:
Syntax:
KGiSL
Page 37
KGiSL
Page 38
Iteration Structure
- A section of code in which an instruction or group of instructions is executed a specific number of times depending on the value of a loop counter. for loop while loop dowhile loop
KGiSL
Page 39
for loop
Syntax:
KGiSL
Page 40
#include <iostream.h> int main() { int count; for(count=1; count <= 100; count=count++) cout << count << " "; return 0; }
KGiSL
Page 41
While loop
The while loop allows programs to repeat a statement or series of statements, over and over, as long as a certain test condition is true. The while loop is an entry-condition loop. Syntax: while (test_condition) { statement; }
KGiSL
Page 42
Example
#include<iostream.h> void main() { int num = 0; while (num < 10) { cout << "*"; num++; } }
KGiSL
Page 43
Dowhile The do-while loop is similar to the while loop, except that the test condition occurs at the end of the loop. The do-while loop is an exit-condition loop. Syntax do { statement; } while (condition);
KGiSL
Page 44
Example
#include<iostream.h> #include<conio.h> void main() { clrscr(); int num=0; do { num++; cout<<num; }while(num<5); getch(); }
KGiSL
Page 45
switch
- switch Statement Its objective is to check several possible constant values for an expression. Syntax: switch (expression) { case 1: group of statements 1; break; case 2: group of statements 2; break;. default: default group of statements }
KGiSL
Page 46
Example
#include <iostream.h> #include<conio.h> void main() { int color ; clrscr(); cout<< Colors; cout<< ******; cout<<1.Red 2.Green 3.Blue; cout<<"Enter an integer to choose a color:; cin>>color;
switch(color)
{ case 1:
case 3:
cout<<"you chose blue color; break; default: cout<<Wrong choice"; } getch(); }
KGiSL
Page 47