Você está na página 1de 9

1.

Pointer To functions
void main()
{
float fun1(float,float,float);
float a,b,c,avg;
float (*ptr)(float,float,float); // pointer to function declaration
ptr=&fun1;
cout<<"Enter a b c (float) ";
cin>>a>>b>>c;
avg=fun1(a,b,c); //normal call
cout<<"\n avg="<<avg<<endl;
avg=ptr(3,2,3); //(*ptr)(3,2,3);// function calling using pointer
cout<<"\n avg="<<avg<<endl;
}
float fun1(float x,float y,float z) { return((x+y+z)/3.0); }
2. Passing function to another function
void main()
{
float add(float,float);
float sub(float,float);
float action(float(*)(float,float),float,float);
// passing a function to other function declaration
float (*ptr)(float,float); // pointer to function declaration
float a,b,c;
cout<<"Enter a b (float) ";
cin>>a>>b;
cout<<"a="<<a<<"\t b="<<b<<endl;
ptr=&add;
c=action(ptr,a,b);
cout<<"\n Sum="<<c<<endl;
cout<<"a="<<a<<"\t b="<<b<<endl;
ptr=&sub;
c=action(ptr,a,b);
cout<<"\n Subtraction="<<c<<endl;
}
float add(float x,float y) { return(x+y); }
float sub(float x,float y) { return(x-y); }
float action(float(*ptr)(float,float),float x, float y)
{
return((*ptr)(x,y));//return(ptr(x,y));
}
3. This pointer
class string
{
int len;
char *p;
public:
string() { len=0; p=NULL; }
string(char *);
string operator +(string &);
string operator <=(string &);
friend void display(string);
};
string :: string(char *x)
{
len=strlen(x);
p=new char[len+1];
strcpy(p,x);
}
string string ::operator +(string & y)
{
string temp;
temp.len=len+y.len;
temp.p=new char[temp.len+1];
strcpy(temp.p,p);
strcat(temp.p,y.p);
return(temp);
}
string string::operator <=(string &y)
{
if(len <= y.len)
return(y);
else
return *this;
}
void display(string s) { cout<<s.p; }
int main()
{
string s1="VNR";
string s2="VIGNANA";
string s3="JYOTHI";
string s4,s5,s6;
s4=s1+s2;
cout<<"S4:";
display(s4);
cout<<endl;
cout<<"S5:";
s5=s1+s3;
display(s5);
cout<<endl;
cout<<"S6:";
s6=s3<=s2;
display(s6);
cout<<endl;
return(0);
}

4. Pointers to objects
class sample
{
int a;
public:
void getdata(void) { cout<<"\n\n Enter a"; cin>>a; }
void putdata() { cout<<"\n a="<<a<<"\n"; }
};
int main()
{
sample s1;
sample *p1;
cout<<"\n Using Object . operator"<<endl;
s1.getdata();
s1.putdata();
cout<<"\n Using Object pointers -> operator"<<endl;
p1->getdata();
p1->putdata();
cout<<"\n Using Object pointers . operator"<<endl;
(*p1).getdata();
(*p1).putdata();
return(0);
}

5. Dynamic creation of objects with constructors


class sample
{
int a;
public:
sample(){a=0;}
sample(int x) { a=x; }
void putdata() { cout<<"\n a="<<a<<"\n"; }
};
int main()
{
sample *p1=new sample(30);
sample *p2=new sample;
cout<<"\n construtor 1"<<endl;
p1->putdata();
cout<<"\n construtor 2"<<endl;
(*p2).putdata();
return(0);
}

6. Pointer to derived classes


class A
{
public:
int a;
void putdata() { cout<<"\n a="<<a<<"\n"; }
};
class B : public A
{
public:
int b;
void putdata() { cout<<"\n a="<<a; cout<<"\n b="<<b<<"\n"; }
};
int main()
{
A *bptr; // base pointer
A obj1;

bptr=&obj1; //base address


bptr->a=10;
bptr->putdata();

B obj2;
bptr=&obj2;
bptr->a=20;
// bptr->b=30; //won't work since bptr is base class A pointer it cannot access d
bptr->putdata();

B *dptr; // derived type pointer


dptr=&obj2;
dptr->b=200;
dptr->a=100;

dptr->putdata();
cout<<" Explicit Casting \n\n";

((B *)bptr)->b=300;
((B *)bptr)->putdata();
A obj3;
B obj4;

A *bptr1;
bptr1=&obj3; // Valid, Base pointer to same class
bptr1=&obj4; // Valid, Base pointer to derived class

B *dptr1;
dptr1=&obj3; // Not Valid. Error. Cannot point to base class object
dptr1=&obj4; // Valid, pointer a same class object
return(0);
}

7. Need of virtual function


class A
{
public: void display() { cout<<"\n Class A"<<"\n"; }
};
class B : public A
{
public: void display() { cout<<"\n Class B"<<"\n"; }
};
int main()
{
A obj1;
B obj2;
A *bptr;

bptr=&obj1;
bptr->display();

bptr=&obj2; //base class pointer


bptr->display();
return(0);
}
/* Output: Class A
Class A */

8. Virtual function
class A
{
public:
void display() { cout<<"\n Class A"<<"\n"; }
virtual void show() { cout<<"Show Class A"<<"\n"; }
};
class B : public A
{
public:
void display() { cout<<"\n Class B"<<"\n"; }
void show() { cout<<"Show Class B"<<"\n"; }
};
int main()
{
A obj1;
B obj2;
A *bptr;

bptr=&obj1;
bptr->display();
bptr->show();

bptr=&obj2; //Base class A pointer points derived class B


bptr->display(); // compile time binding
bptr->show(); // runtime binding
// obj1.show();
// obj2.show();
return(0);
}
/* Output: Class A
Show Class A
Class A
Show Class B
*/
display () is not virtual so always A::display (); executed

When we use the same function name in both and derived classes, the function in base
class declared as virtual using keyword virtual preceding its normal declaration.

When a function is made virtual C++ determines which function to use at run time based
on the type of object pointed to by the base pointer, rather than the type of the pointer.

Thus by making the base pointer to point to different objects, we can execute different
versions of the virtual functions

We can use dot operator to access virtual functions like other member functions. But run
time polymorphism is achieved only when a virtual function is accessed through a pointer
to the base class
9. Pure Virtual function
class student
{
public:
int no;
char name[20];
virtual void getdata();
virtual void show();
};
class result : public student
{
char res[10];
float percent;
public:
void getdata();
void show();
};
void student::getdata() { //empty body }
void student::show() { // empty body }
void result::getdata()
{
cout<<"Enter No ";
cin>>no;
cout<<"\n Enter name";
cin>>name;
cout<<"\n Enter Student Result pass or fail";
cin>>res;
cout<<"\n Enter Student Percentage ";
cin>>percent;
}
void result::show()
{
cout<<" No Name Result Percentage \n";
cout<<no<<"\t"<<name<<"\t"<<res<<"\t"<<percent<<endl;
}
int main()
{
student *bptr;
result obj1;
bptr=&obj1;
bptr->getdata();
bptr->show();
return(0);
}
A Pure virtual function is a function declared in a base class that has no definition relative
to the base class.
In such cases, the compiler required each derived class to either define the function or
redeclare it as a pure virtual function.
A class containing pure virtual functions cannot be used to declare any objects on its own
(called as Abstract base classes).
The main objective of an abstract class is to provide some traits to the derived classes and
to create a base pointer required for achieving runtime polymorphism.

10. Pure Virtual function 2


class student
{
public:
int no;
char name[20];
virtual void getdata() =0;
virtual void show()=0;
};
class result : public student
{
char res[10];
float percent;
public:
void getdata();
void show();
};
void result::getdata()
{
cout<<"Enter No ";
cin>>no;
cout<<"\n Enter name";
cin>>name;
cout<<"\n Enter Student Result pass or fail";
cin>>res;
cout<<"\n Enter Student Percentage ";
cin>>percent;
}
void result::show()
{
cout<<" No Name Result Percentage \n";
cout<<no<<"\t"<<name<<"\t"<<res<<"\t"<<percent<<endl;
}
int main()
{
student *bptr;
result obj1;
bptr=&obj1;
bptr->getdata();
bptr->show();
return(0);
}

The virtual function may be equated to zero if it does not have a function definition.
Such functions are called as do-nothing functions or pure virtual function.
Ex: virtual void getdata() =0 ;

Você também pode gostar