OBJECT ORIENTED
PROGRAMMING
First Program
# include < iostream.h>
int main()
{
cout<<”Hello World”;
}
• # include <iostream.h> • int main()
– Sentences beginning with – Here the program starts
# are directives for execution. main is
preprocessor. followed by a pair of ().
– They are not executable – The contents of the
code lines but are just main function are
indicators to the compiler. written within {}.
– # include <iostream.h> • cout<<”Hello World”;
tell’s the compiler’s – cout is declared in
preprocessor to include iostream.h header file,
the iostream standard so in order to use it
header file. should be included.
– This file includes the – << is known as insertion
declarations of the or put operator.
standard input-output
library in C++.
VARIABLES
• Declaration of variables
– <data type > variable_name
• int a;
• float a;
• Initialization of variables
– <data type > variable_name = initial_value;
• int a = 15;
SCOPE
• Global variable:
– It can be used anywhere in the code after its
declaration.
• Local variable:
– It is limited to the code level in which it is declared.
– If a variable is declared at the beginning of a main
function it can be used only in the main.
• External Scope:
– This causes the variable to be visible not only in the
same source file but in all the other files that is
linked to it.
STRUCTURE OF C++
void person:: getdata(void)
• Include files {
cout <<”enter name”;
• Class declaration cin >> name;
• Member functions cout << “enter age”;
cin >>age;
• Main functions }
void person :: display(void)
{
EXAMPLE cout << “\n Name :” << name;
# include<iostream.h> cout << “\n Age :” << age;
class person }
{
char name[30]; int main()
int age; {
public : person p;
void getdata(void); p.getdata();
void display(void); p.display();
TOKENS
• Tokens:
– Smallest individual units in a program.
• The different types of tokens are:
– Keywords
– Identifiers
– Constants
– Strings
– Operators
• Keywords
– They are reserved identifiers and cannot be used for variable
names and other user defined elements.
– E.g. Auto, break, case, char, const, continue, default, switch,
new, if, goto
• Identifiers and Constants
– Identifier refers to names of variables, functions, arrays,
classes.
– Constants refer to fixed values
– Integer number
• 1776
– Floating point
• 6.89
– Character and String
• ‘b’
• “lo”
– Escape Sequence
• \n new line
• \r carriage return
• Defined Constants
# define preprocessor directive]
# define identifier value
E.g.
# define PI 3.14
# define width 100
int circle;
circle=2*PI*width;
• Declared Constants
const int width=700;
const zip=90; if type not specified assumed to be int
C++ DATA TYPES
• Structure
USER • Union
DEFINED DATA •
•
Class
Enumeration
TYPES
• Integral Type int, char
• Void
BUILT IN TYPE • Float Type float, double
• Array
• Function
DERIVED • Pointer
•
TYPE reference
USER DEFINED DATA
TYPES
• Enumerated Data Type:
– enum shape {circle, square, triangle}
– enum color {red, blue, green}
• shape ellipse; // ellipse is of type shape
• color background; // background is of type
color
• color background = blue; // allowed
• color background =7 // not correct
• color background= (color)2
• enum color {red ,blue=6, green=6}
• ARRAYS
– char names[15]=”Ama University“
• POINTERS
– int x = 90;
– int *p;
– p = &x;
– *p = 90;
• SYMBOLIC CONSTANT
There are two ways of making constants
• Using the qualifier constant
• Using enum keyword
Const values should be initialized
const int size=90;
To give const value an external linkage, so that it can be
referenced from another file we have to declare it with extern
extern const total=890;
enum {x=90,y=70,z=78};
• TYPE CASTING
int i;
float f=8.90
i=(int) f;
cout<<i;
Declaration of Variable
• C++ allows the declaration of variable
anywhere in the scope.
• A variable can be declared at the place of its
first use.
– int a=9;
Dynamic Initialization of variables
• It helps initialization of variables at runtime
• float area=7.9*rad*rad;
Reference Variables
• A reference variable gives an alias name
for a previously defined variable
• Datatype &referencename=variablename;
• E.g.
float total=100;
float &sum=total;
sum is an alternate name for total. Both are
referring to the same memory location.
total==100;
total=total+10;
will change total and sum to 110.
OPERATORS IN C++
• Assignment (=)
– int a;
– a=5;
• Chained Assignment
– int x,y;
– x=y=70;
– float x=y=9.9 //wrong
• Embedded Assignment
– x= ( y=50)+5; //y=50 x=55
• Compound Assignment
– x+=8 //x=x+8
• Arithmetic Operator (+, - , *, /, %)
• Compound Assignment operators (+=, -
=,*=, /=)
• Relational operator (==, ! =,>, <, <=, >=)
• Logic Operator (!, && , || , !=)
• Conditional Operators (? : )
– 7==5 ? 4 :3 Ans: 3
– 7==5+2 ? 4:3 Ans: 4
• sizeof()
– Returns the size in bytes for that type of
object
– A=sizeof(char) Ans : 1
• Pointer operator (*) this is used to get the
content of the address operator pointing to
particular cell or element.
• Address operator (&) The address
operator is used to get the address of
another variable in an indirect manner.
• Increment and decrement operator (++,
--)
• new and delete : These operators helps
to carry out dynamic memory allocation
and deallocation.
• Scope resolution operator (::)
# include <iostream.h>
int m=10 // global variable
int main()
{
int m =20;// m is again declared but local to main
{
int k=m;
int m=30; //m again declared local to inner block
cout <<”we r in inner block”;
cout <<” k is”<<k;
cout<<” m is “<<m;
cout<”::m is”<<::m;
}
cout <<”we r in outer block”;
cout<<” m is “<<m;
cout<”::m is”<<::m;
• Memory management operator
An Object can be created by using the new operator
and destroyed by using delete.
Pointervariable=new datatype;
Pointervariable is used to hold the address of variable.
int *p;
p=new int;
*p=78;
Pointervariable=new datatype(value);
Pointervariable=new datatype[size];
delete pointervariable;
delete[size]pointervariable;
• Typecast Operator
typename ( expression);
average = sum / (float)I;