18bit23c U4
18bit23c U4
TEXT BOOK
1. Ashok N Kamthane, “Object Oriented Programming with ANSI and Turbo
C++”, Pearson Education Publications, 2006
Example:
• int *a;
• int *b;
• float*c;
• It inform to the compiler that hold the address of any variable.
• The in-direction(*)operator is also called de-reference
operator.
• Where as a pointer indirectly access to their own values.
• The (&) is the address operator represented the address of a
variable.
• The address of any variable is a whole number.
Example:
• Display the value and address of the variable using pointer.
#include<iostream.h>
#include<conio.h>
void main()
{
int *p;
int x=10;
p=&x;
cout<<x<<&x;
cout<<*p<<&p;
}
Void pointer
• Pointer can be also be declared as void type.
• Void pointer cannot be reference without explicit type
conversion.
• A void pointer can pointer any type of variable with proper
type casting.
Example:
#include<iostream.h>
#include<conio.h>
int p;
float d;
char c;
void *pt=&p;
void main()
{
*(int*)pt=12;
cout<<p;
pt=&d;
*(float*)pt=5.4;
cout<<d;
pt=&c;
*(char*)pt=’x’;
cout<<c;
}
Pointer to class:
Syntax:
Class name *pointer variable name
Example:
Student *str;
Example:
#include<iostream.h>
#include<conio.h>
Classman
{
public:
char name[20];
int age;
voidgetdata(char s[10],int a)
{
name =s;
age=a;
}
Voidputdata()
{
cout<<name<<age;
}
};
void main()
{
Man *ptr,m;
ptr=&m;
ptr->getdata(“xyz”,m);
ptr->putdata();
}
Pointer to object:
Syntax:
Class name*ptr variable name;
Eg:
Student*str;
Example
#include<iostream.h>
#include<conio.h>
class bill
{
intqty;
float price;
float amount;
public:
{
Voidgetdata(inta,floatb,float c)
{
qty=a;
price=b;
amount=c;
}
void show()
{
cout<<”QUANTITY:”<<qty<<”\n”;
cout<<”PRICE”<<price<<”\n”;
cout<<”AMOUNT”<<amount<<”\n”;
}
};
int main()
{
clrscr();
bill s;
bill*ptr;
ptr=&s;
ptr->getdata(45,10.25,45*10.25);
(*ptr).show();
return 0;
}
This pointer:
Example
• Write a program to declare a pointer to the base class and
access the member variable of base and derived class.
# include<iostream.h>
#include<conio.h>
class A
{
Public:
int b;
void display()
{
cout<<”b=”<<b<<”\n”;
}
};
class B:public A
{
public:
int d;
void display()
{
cout<<”b=”<<”\n’<<”d=”<<d<<”\n”;
}
void main()
{
clrscr();
A*CP;
A base;
CP=&base;
CP->b=100;
// CP->d=200;Not accessible
cout<<”\n CP points to the base object\n”
CP->display();
B b;
Cout<<”\n CP points to the derived class\n”
CP=&b;
CP->b=150;
// CP->d=300; Not accessible
CP->display();
return 0;
}
Pointer to the derived class
#include<iostream.h>
#include<conio.h>
class A
{
public:
int b;
void display()
{
cout<<”b=”<<b<<”\n”;
}
};
class B : public A
{
public:
int d;
void display()
{
cout<<”\t b=”<<”\n”<<”\t d=”<<d<<”\n”;
}
};
void main()
{
clrscr();
B*CP;
B b;
CP=&b;
CP->b=100;
CP->d=350;
cout<<”\n CP points to the derived object \n”;
CP->display();
return 0;
}
Array
#include<iostream.h>
#include<conio.h>
class arraytest
{
int a[10];
public:
void get();
void display();
};
void arraytest::get()
{
for(int i=1;i<=10;i++)
{
cin>>a[i];
}
}
void arraytest::display()
{
for(int i=1;i<=10;i++)
{
cout<<a[i];
}
}
void main()
{
arraytest a1;
a1.get();
a1.display();
}
Array of classes :-
for(i=1;i<=n;i++)
{
S[i].get();
}
for(i=1;i<=n;i++)
{
S[i] display();
}
}
C++ and Memory models :-
• The memory models sets the supportable size of code and data.
• Before computing and linking the source code we have to
specify the appropriate memory model.
• C++ always use different segments for code and data.
The different types of memory models used in
C++ are as follows :
1. Tiny :-
• The tiny model is absolutely premium all four segment
registers(CS,DS,SS,ES) are initialized with same address and all
addressing is accomplished using 16-bits total memory capacity
is 64 kb.
• The code data and stack must fit with in the same 64kb segment
• Programme are executed quickly is this model is selected.
2. Small :-
• All code should fit in a single 64kb segment.
• All pointers are 16bit in length execution speed is same as single
model.
• This model is used for average size programs near pointers are
always used.
3. Medium :-
• All data fit in a single 64kb segment.
• The code is allowed to use multiple segments.
• All jumps and calls require 32bits address.
• In this model access to data is passed program execution is
slowwith their model.
• Far pointers are used for code.
4. Compact :-
• All code fit in 64kb segment to the data can use multiple
segment all pointers to data and 32bits.
• But jumps and calls used 16bits slow action to data and quick
code execution will be observed this model.
• For pointers are preferred for data.
5. Large :-
• Both code and data allowed to multiple segment.
• All pointers are 32bits code execute is slow.
• This model is preferred for very big programs.
• For pointers are used for both code and data.
6. Huge :-
• Both code and data are allowed to use multiple segments
• Every pointers is 32bits in length cod execution is the slowest.
• The huge model permits multiple data segments 1MB code
segments and 64kb for stack.
The pointers and code pointers are far
Syntax
Delete pointer variable name;
Delete [element size] pointer variable name;
Example
Delete a;
Delete[50] a;
Size of operator
• The sizeof operator is used to returns the size of your variable
bytes.
Syntax
• sizeof (variable name);
Example
• Cout<<sizeof(a);
Few points regarding : new and delete
operators are :-
• The new operator not only creates an object but also allocates
memory.
• The new operator allocates correct amount of memory from
the heap that also called a free store.
• The object created and memory allocated using new operator
should be deleted by the delete operator.
• The delete operator not only destroys the object but also
returns the allocated memory.
• The new operator creates an object and its remains in the
memory until it is released using delete operator.
• If we sent a null pointers to delete operator it is secure using
delete to zero(0) has no result.
• The statement delete x does not destroy the pointer x.
• It destroy the object.
• If the object created is not deleted it occupy the unnecessarily.
• It is a good habit to destroy the object and release the system
resources.
Difference between new and malloc()
New Malloc()
Syntax:
• delete ptr;
• It destroy the object pointed by pointer ptr.
Example
#include<iostream.h>
#include<conio.h>
class data
{
int x,y;
public:
data()
{
cout<<”\n construction”;
x=10;
y=50;
}
~data(){cout<<”\n destructor”;}
void display()
{
cout<<”\n*x=”<<x;
cout<<”\n y=”<<y;
}
};
void main()
{
clrscr();
data*d;
d=new data;
d→display();
delete d;
}
Binding ,polymorphism and virtual functions
• Binding:-
• In c++ functions can be bound either at complied time (or) at
run time designing the function call at compile time is called
compile time (or)early(or)static binding.
• Designing function call at run time is called as run
time(or)late(or)dynamic binding .
• Dynamic binding permits the decision of choosing suitable
function until run time.
Static or early binding:-
• Similar function name are used at many places but during their
references their position is indicates explicitly .
• Their ambiguities are fixed at compile time.
Example:
class first
{
int d;
public:
void display()
{cout<<d;}
};
int k;
public:
void display()
{
cout<<k;
}
}
2. Dynamic (or) late binding :
Function
Virtual function Operator overloading
overloading
Virtual Function
• The member function of the class is rarely used for doing any
operation.
• Such function are called do-nothing functions, dummy
function or pure virtual function.
• The pure virtual function are always virtual function.
• There are defined with null body after declaration of pure
virtual function in a class. The becomes abstract class.
• It cannot be used for declare any object.
The pure function can be declared as follows:
Syntax
• virtual void display()=0;
• A pure virtual function declared in base class cannot be used
for any operations.
• The class containing pure virtual function cannot be used
declared object.
• Such class are known as abstract class (or) pure abstract class.
• The class is derived from pure abstract classes are required to
redeclared pure virtual function.
• All other derived classes witout pure virtual function are called
concreate classes.
• These classes can be used to create objects.
#include<iostream.h>
#include<conio.h>
class first
{
protected;
int b;
public:
first(){b=10}
virtual void display()=0;
};
Class second : public first
{
int d;
public:
second(){d=20}
void display(){ cout<<”\nb=”<<b<<”d=”<<\n”
}
int main()
{
clrscr();
first*P;
second *S;
p=&S;
p->display();
return 0;
}
Constructor and virtual function