Object Oriented Programming
using C++
Topic: C++ Basics
Mr.B. Srinu
Contents
Structure of a C++ Program
Data Types
Operators
Strcuture of C++ Program
Header File Section
Preprocessor Directives
Global Declaration Section
Class definition
Member Function definition section
Main function section
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;
}};
Simple C++ Program
void Circle::display()
{
area = PI * r*r;
cout<< "Area of circle is " <<area;
}
int main ( )
{
Circle c1;
[Link]();
[Link]();
return 0;
}
Datatypes
Primitive Data Type Categories:
Integer
Character
Floating point numbers
Boolean
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
Variables
Named location in memory, used to hold a value
that may be modified by the program.
Syntax:
type variable_list;
Example:
int i,j,l;
short int si;
unsigned int ui;
double balance, profit, loss;
Operators
Type of Operation to be carried out
Types
Unary , Binary , Ternary
Operators in C++ are:
Arithmetic Operators
Relational Operators
Bit-wise Operators
Conditional operator (?)
Assignment Operator
& , * operators
Arithmetic Operators
int i=10, j=20;
k=i++ + ++j => ?
k=--i + j-- => ?
K= i++ + --i + ++j + --i => ? ( i,j,k)
Relational Operators
int i=20 , j=6
bool x=i>j => true
bool x=i<j => false
bool x=(i==j) => false
bool x=(i!=j) => true
Logical Operators
bool x=true , y=false
z= x && y => false
z= x || y => true
z= !x => false
Bitwise Operators
int a=2 , b=4
c=a&b =>0
c=a|b =>6
c=a^b =>6
c=~a =>-3
c=a<<1 =>4
c=a>>1 => 1
Conditional Operators
Ternary Operator
Alternate to if-then-else form
Exp1 ? Exp2 : Exp3;
int x = 10;
int y = x>9 ? 100 : 200;
y is assigned the value 100.
Other Operators
Assignment Operator:
variable_name = expression;
sizeof operator:
double f;
sizeof f => 8
sizeof(int)) => 4
The & and * Pointer Operators:
int a=10; Adress a =>100
int *p = &a; Address p => 200
int **q= &p; Address q=> 300
&a => 100 , *p => 10
&p => ? *q => ? q => ?
**q => ? *(&q)
Summary
Structure of C++ Program
Data Types
Operators