C++ Programming Basics
Chapter 2:
Ms. Munazza Mah Jabeen
Assistant Professor of Computer Science
Basic Program Construction:
#include <iostream>
using namespace std;
int main()
{
cout << “Every age has a language of its ownn”;
return 0;
}
Basic Program Construction:
• Directives
• Preprocessor Directives
• Header Files
• Include Two Ways:
• #include< >
• #include “ ”
• The using Directive
• Namespaces
• Always Start with main()
• Functions
• Function Name
• Braces and the Function Body
• Program Statements
• Whitespace
• String Constants
• Statement Terminator
• Comments
• //
• /* … */
Input / Output Statements:
#include <iostream>
using namespace std;
int main()
{
int ftemp; //for temperature in Fahrenheit
cout << “Enter temperature in Fahrenheit: “;
cin >> ftemp;
int ctemp = (ftemp-32) * 5 / 9;
cout << “Equivalent in Celsius is: “ << ctemp << ‘n’;
return 0;
}
Language Basics:
Data Types
• Numeric
• Non-floating point
• Short
• Integer
• Long
• Floating –Point
• Float
• Double
• Non-Numeric
• Character
• String
• Boolean
Language Basics:
• Data Type:
• Conversion
• Casting
• Declarations
• Definitions
• Variable Names
• Initialization
• Assignment Statements
• Variables Defined at Point of Use
• The const Qualifier
• The #define Directive
#include <iostream>
using namespace std;
#define PI 3.142
main()
{
// const float PI = 3.142;
float radius=0.0, area=0.0;
cout << "Enter Radius of the Circle == > ";
cin >> radius;
area = PI * radius * radius ;
cout << " Radius of Circle is " << radius << " Area of the
Circle is " << area << endl;
}
#include<iomanip>
#include<iostream>
using namespace std;
main()
{
cout << "Lahore" << setw(20) << setfill('.') << "500 Km"<<endl;
}
IOMANIP:
• The IOMANIP Header File
• The endl Manipulator
• The setw() Manipulator
• The setfill() Manipulator
Escape Sequences:
Operators:
• Arithmetic Operators ( + , - , / , * , % )
• Increment / Decrement ( ++ , -- )
• Prefix / Postfix ( a++ , ++a , a-- , --a )
• Assignment Operator ( = )
• Arithmetic Assignment Operator ( += , -= , /= , *= , %= )
• Relational operators ( > , < , >= , <= , == , != )
• Logical Operators
• And / Or / Not ( &&, || , ! )
Header Files and Library Files:
#include <iostream> //for cout, etc.
#include <cmath> //for sqrt()
using namespace std;
int main()
{
double number, answer; //sqrt() requires type double
cout << “Enter a number: “;
cin >> number; //get the number
answer = sqrt(number); //find square root
cout << “Square root is “
<< answer << endl; //display it
return 0;
}

C++ programming basics

  • 1.
    C++ Programming Basics Chapter2: Ms. Munazza Mah Jabeen Assistant Professor of Computer Science
  • 2.
    Basic Program Construction: #include<iostream> using namespace std; int main() { cout << “Every age has a language of its ownn”; return 0; }
  • 3.
    Basic Program Construction: •Directives • Preprocessor Directives • Header Files • Include Two Ways: • #include< > • #include “ ” • The using Directive • Namespaces • Always Start with main() • Functions • Function Name • Braces and the Function Body • Program Statements • Whitespace • String Constants • Statement Terminator • Comments • // • /* … */
  • 4.
    Input / OutputStatements: #include <iostream> using namespace std; int main() { int ftemp; //for temperature in Fahrenheit cout << “Enter temperature in Fahrenheit: “; cin >> ftemp; int ctemp = (ftemp-32) * 5 / 9; cout << “Equivalent in Celsius is: “ << ctemp << ‘n’; return 0; }
  • 5.
    Language Basics: Data Types •Numeric • Non-floating point • Short • Integer • Long • Floating –Point • Float • Double • Non-Numeric • Character • String • Boolean
  • 6.
    Language Basics: • DataType: • Conversion • Casting • Declarations • Definitions • Variable Names • Initialization • Assignment Statements • Variables Defined at Point of Use • The const Qualifier • The #define Directive #include <iostream> using namespace std; #define PI 3.142 main() { // const float PI = 3.142; float radius=0.0, area=0.0; cout << "Enter Radius of the Circle == > "; cin >> radius; area = PI * radius * radius ; cout << " Radius of Circle is " << radius << " Area of the Circle is " << area << endl; }
  • 7.
    #include<iomanip> #include<iostream> using namespace std; main() { cout<< "Lahore" << setw(20) << setfill('.') << "500 Km"<<endl; } IOMANIP: • The IOMANIP Header File • The endl Manipulator • The setw() Manipulator • The setfill() Manipulator
  • 8.
  • 9.
    Operators: • Arithmetic Operators( + , - , / , * , % ) • Increment / Decrement ( ++ , -- ) • Prefix / Postfix ( a++ , ++a , a-- , --a ) • Assignment Operator ( = ) • Arithmetic Assignment Operator ( += , -= , /= , *= , %= ) • Relational operators ( > , < , >= , <= , == , != ) • Logical Operators • And / Or / Not ( &&, || , ! )
  • 10.
    Header Files andLibrary Files: #include <iostream> //for cout, etc. #include <cmath> //for sqrt() using namespace std; int main() { double number, answer; //sqrt() requires type double cout << “Enter a number: “; cin >> number; //get the number answer = sqrt(number); //find square root cout << “Square root is “ << answer << endl; //display it return 0; }