Download - Introduction to c++
![Page 1: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/1.jpg)
Introduction to C++BY HIMANSHU KAUSHIK
![Page 2: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/2.jpg)
What is c++ ?
C++ is a object-oriented language
It is the “successor” to C, a procedural language (the “++” is called the successor operator in C++)
C was derived from a language called B which was in turn derived from BCPL
C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs
C++ was developed in the early 1980’s by Bjarne Stroustrup of AT&T Bell Labs.
Most of C is a subset of C++
![Page 3: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/3.jpg)
People & Programs
User: an individual who runs, or executes, a program
Programmer: an individual who creates, or writes, a program
![Page 4: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/4.jpg)
C++ Programming
Consists of…Declarations
Define the use of various identifiers, thus creating the elements used by the program (computer)
Statements Or executable statements, representing actions the computer will take on
the user’s behalf
![Page 5: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/5.jpg)
Identifiers
Names for various entities used in a program; used for...
Variables: values that can change frequently Constants: values that never changes Functions: programming units that represents
complex operations Parameters: values that change infrequently
![Page 6: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/6.jpg)
A Simple c++ Program
#include <iostream.h>int main()
{// Declarations
// Statements
return 0;
}
![Page 7: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/7.jpg)
The Header File
#include <iostream.h>int main()
{
// Declarations
// Statements
return 0;
}
Compiler directive: Tells the compiler what to do before compiling
This one includes source code from another file
![Page 8: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/8.jpg)
The Main Statement
#include <iostream.h>
int main(){
// Declarations
// Statements
return 0;
}
Header for main function States…
data type for the return value
identifier for function list of arguments between
parenthesis(none for this function)
![Page 9: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/9.jpg)
The Braces
#include <iostream.h>int main()
{// Declarations
// Statements
return 0;
}
Braces enclose the body of the function
They represent the start and end of the function
![Page 10: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/10.jpg)
The Declaration Statement
#include <iostream.h>int main()
{
// Declarations
// Statementsreturn 0;
}
Declarations and statements
Main body of function (or main part)
“//” represents the start of a comment
![Page 11: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/11.jpg)
The return Statement
#include <iostream.h>int main()
{// Declarations
// Statements
return 0;}
Return statementspecifies the value
the function returnsAll (almost)
declarations and statements end with a semi-colon “;”
![Page 12: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/12.jpg)
Sample C++ Program
#include <iostream.h>void main()
{int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
What is the Output of this Program?
![Page 13: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/13.jpg)
The DataTypes
#include <iostream.h>void main()
{int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
Variable declarationThe identifier number is
declared as being of data type int, or integer
![Page 14: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/14.jpg)
![Page 15: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/15.jpg)
The Count ?
#include <iostream.h>void main()
{int number;
cout << “Enter a number” << endl;cin >> number;
cout << “You entered: “ << number << endl;
}
coutthe output statement for C++
Note the direction of “<<“endl represents an end-of-line
![Page 16: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/16.jpg)
Cin ?
#include <iostream.h>void main()
{int number;
cout << “Enter a number” << endl;
cin >> number;cout << “You entered: “ << number << endl;
}
cinthe input statement for C++Note the direction of “>>”
![Page 17: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/17.jpg)
Copy That Down
#include <iostream.h>void main()
{int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
![Page 18: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/18.jpg)
Value Assignment In c++
Assignment is an operation that assigns the value of an expression to a variable
Ex. Total = 2 + 3 + 5First, the expresssion “2 + 3 + 5” is
evaluatedThen, this value is assigned to the
variable “Total”
![Page 19: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/19.jpg)
Assignment When a variable is declared, space is
allocated in the computer’s memory for the variable
Each data type requires a different number of bytes in memory for storing a variable
int - 2float - 4double - 8char, bool - 1
![Page 20: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/20.jpg)
Arithmetic Operations
Addition: 2 + 3Subtraction: 5 - 2Multiplication: 10 * 4Division: 12 / 3
![Page 21: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/21.jpg)
I Have A problem for you
Problem: To determine the average of three numbers
Task: Request, from the user, three numbers, compute the average and the three numbers, and print out the original values and the computed average
Do it!You have 20 minutes!
![Page 22: Introduction to c++](https://reader033.vdocuments.us/reader033/viewer/2022042700/555e35ced8b42aad058b47ed/html5/thumbnails/22.jpg)
Any Queries ?