Lecture 03 C++ Programming BasicsComparison between C++ & C:i) input/output statementii) data typeiii) arithmetic operator...
C++ vs CC++ is a superset of C.C++ was originally called “C with classes”.
Basic C++ Program Construct
Basic C++ Program Construct              A Sample Program// first.cpp#include <iostream>//preprocessorusing namespace std;...
CommentsC Style:/* This program demonstrate    function overloading */C++ Style:// This program demonstrate// function ove...
Preprocessor Directive                    #includeC Style:#include<stdio.h>C++ Style:#include<iostream.h> OR THE FOLLOWING...
Input and OutputC Input and Ouput:#include<stdio.h>scanf(“%i”, &num);printf(“The number is %in“, num);C++ Input and Ouput:...
Stream I/OInsertion and Extraction
ConstantsC-Style Symbolic Constant:#define PI 3.14159We can’t specify the data type!                               What is...
Boolean Type• Integer, character, and floating-point types are available  both in C and C++.• Boolean type is available in...
Boolean Type                                        Void Main( )                                        { int num; bool fl...
Type Conversion : Order of Data Types Data Type      Order long double Highest double float long int short char        Low...
Automatic Type Conversion• When two operands of different types are encountered in  the same expression, the lower-type va...
The   setw Manipulator#include <iostream.h>#include <iomanip.h>     // for setwint main(){  long pop=2425785;  cout << set...
The <cmath>                Standard Library Functions// sqrt.cpp// demonstrate sqrt() library function#include <iostream> ...
Header and Library Files
Upcoming SlideShare
Loading in...5
×

Lecture03

244

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
244
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture03

  1. 1. Lecture 03 C++ Programming BasicsComparison between C++ & C:i) input/output statementii) data typeiii) arithmetic operatorsiv) header files
  2. 2. C++ vs CC++ is a superset of C.C++ was originally called “C with classes”.
  3. 3. Basic C++ Program Construct
  4. 4. Basic C++ Program Construct A Sample Program// first.cpp#include <iostream>//preprocessorusing namespace std; //directiveint main(){ int ftemp; // temperature in Fahrenheit cout << "Enter temperature in Fahr.: "; cin >> ftemp; int ctemp = (ftemp - 32) * 5 / 9; cout << "Equivalent in Celsius is: " << ctemp << endl; return 0;
  5. 5. CommentsC Style:/* This program demonstrate function overloading */C++ Style:// This program demonstrate// function overloading
  6. 6. Preprocessor Directive #includeC Style:#include<stdio.h>C++ Style:#include<iostream.h> OR THE FOLLOWING ANSI STANDARD#include<iostream>using namespace std;
  7. 7. Input and OutputC Input and Ouput:#include<stdio.h>scanf(“%i”, &num);printf(“The number is %in“, num);C++ Input and Ouput:#include<iostream.h>cin >> num;cout << “The number is “, num << endl;
  8. 8. Stream I/OInsertion and Extraction
  9. 9. ConstantsC-Style Symbolic Constant:#define PI 3.14159We can’t specify the data type! What is a Variable ?C++ const Qualifier: What is anconst float PI = 3.14159; Identifier? Rules for declaring an identifier?Recommended ! Int Sum1 ; Float 1Salaray
  10. 10. Boolean Type• Integer, character, and floating-point types are available both in C and C++.• Boolean type is available in C++, but not in C.• Variables of type bool can have only two possible values: true and false (Boolean Values).• Even though a single bit is enough to store variables of type bool, compilers often store them as integers for ease of processing.
  11. 11. Boolean Type Void Main( ) { int num; bool flag = true;void main() Cin>> num;{ if (num > 0 ) bool flag = true; flag = true; else if (flag) flag = false; cout<<“Hello Halizah”<<endl; if (flag == true ) cout<<“positive else number”; cout<<“Hello World!”<<endl; else} cout “negative number”; }
  12. 12. Type Conversion : Order of Data Types Data Type Order long double Highest double float long int short char Lowest
  13. 13. Automatic Type Conversion• When two operands of different types are encountered in the same expression, the lower-type variable is automatically converted to the higher type variable. int denominator; float numerator, quotient; ... quotient = numerator/denominator;• denominator is converted from int to float before the division.
  14. 14. The setw Manipulator#include <iostream.h>#include <iomanip.h> // for setwint main(){ long pop=2425785; cout << setw(8) << "LOCATION" << setw(12) << "POPULATION" << endl << setw(8) << "Portcity" << setw(12) << pop << endlreturn 0;}Output of the program:LOCATION POPULATIONPortcity 2425785
  15. 15. The <cmath> Standard Library Functions// sqrt.cpp// demonstrate sqrt() library function#include <iostream> // for cout, etc.#include <cmath> // for sqrt()using namespace std;int main(){ double number, answer; cout << "Enter a number: "; cin >> number; answer = sqrt(number); cout << "Square root is " << answer << endl; return 0;}
  16. 16. Header and Library Files
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×