Date post: | 22-May-2015 |
Category: |
Software |
Upload: | himanshu-kaushik |
View: | 235 times |
Download: | 2 times |
Introduction to C++BY HIMANSHU KAUSHIK
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++
People & Programs
User: an individual who runs, or executes, a program
Programmer: an individual who creates, or writes, a program
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
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
A Simple c++ Program
#include <iostream.h>int main()
{// Declarations
// Statements
return 0;
}
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
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)
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
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
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 “;”
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?
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
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
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 “>>”
Copy That Down
#include <iostream.h>void main()
{int number;
cout << “Enter a number” << endl;
cin >> number;
cout << “You entered: “ << number << endl;
}
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”
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
Arithmetic Operations
Addition: 2 + 3Subtraction: 5 - 2Multiplication: 10 * 4Division: 12 / 3
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!
Any Queries ?