Object Oriented Programming
using C++
Topic: C++ Basics
Mr.B.Srinu
Contents
Program Execution Steps
Structure of a C++ Program
Namespaces
Structures
Class and Objects
Simple Program
Data Types
Program Execution Steps
Strcuture of C++ Program
Header File Section
Preprocessor Directives
Global Declaration Section
Class definition
Member Function definition section
Main function section
Strcuture of C++ Program
Namespace:
A namespace is a declarative region that
provides a scope to the identifiers (names of
the types, function, variables etc) inside it.
Multiple namespace blocks with the same
name are allowed.
Structure of C++ Program
Structure in C:
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
Analogy in C++ is class
Differences:
Member functions inside structure
Direct Initialization
Structure of C++ Program
Class declaration:
class <name of class>
{
access-specifier:
data members;
member functions();
};
return_type
class_name::function_name(parameters)
{ // body }
Simple C++ Program
#include <iostream.h>
Using namespace std;
#define PI 3.142
float r;
class Circle // Class definition
{
private:
float area; // Data Member
public:
void read() // Member function
{
cout<"Enter the radius of the circle:";
cin>>r;
}
void display();
};
Simple C++ Program
void Circle::display()
{
area = PI * r*r;
cout<< "Area of circle is " <<area;
}
int main ( )
{
Circle c1;
c1.read();
c1.display();
return 0;
}
Datatypes
Primitive Data Type Categories:
Integer
Character
Boolean
Floating point numbers
Void
Datatype modifiers:
signed / unsigned
Short / long
Datatypes
DATA TYPE SIZE (IN BYTES) RANGE
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967,295
int 4 -2,147,483,648 to 2,147,483,647
long int 4 -2,147,483,648 to 2,147,483,647
long long int 8 -(2^63) to (2^63)-1
unsigned long long int 8 0 to 18,446,744,073,709,551,615
signed char 1 -128 to 127
unsigned char 1 0 to 255
float 4
double 8
long double 12
Summary
Program Excecution Steps
Structure of C++ Program
Data Types