Introduction To C++: An Object - Oriented Language
Introduction To C++: An Object - Oriented Language
Created by Bjarne Stroustrup at Bell Labs in the 1980's and called C with
Classes.
Inherit all ANSI C directives
Inherit all C functions
You don’t have to write OOP programming in C++
Example :
// This program outputs the message
//
// Hello!
//
// to the screen
#include <iostream.h>
int main()
{
cout <<"Hello!"<< endl;
return 0;
}
Program: Average
#include <iostream>
using namespace std;
int main() {
int x;
int y;
cout <<"Enter two numbers \n";
cin >> x >> y;
cout <<"Their average is: ";
cout << (x + y)/2.0 << endl;
return 0;
}
Useful Concepts in C++
Data Encapsulation
Data Abstraction
Inheritance
Polymorphism
Operator overloading
Function templates
Virtual functions
Exception Handling
Phases of C++ Programs
Program is created in
1. Edit Editor Disk
the editor and stored
on disk.
Preprocessor Disk Preprocessor program
2. Preprocess processes the code.
Compiler creates
Compiler Disk object code and stores
3. Compile Linker
it on disk.
Linker links the object
Disk code with the libraries,
creates a.out and
4. Link Loader
Primary
Memory
stores it on disk
5. Load Disk ..
Loader puts program
in memory.
..
..
6. Execute Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
.. storing new data
..
.. values as the program
executes.
Parts of a C++ Program
// sample C++ program comment
gallons = static_cast<int>(area/500);
avg = static_cast<double>(sum)/count;
Older Type Cast Styles
double Volume = 21.58;
int intVol1, intVol2;
intVol1 = (int) Volume; // C-style
// cast
intVol2 = int (Volume); //Prestandard
// C++ style
// cast