Monday, 26 February 2018

Object Oriented Programming: 6 to 8


 No: 06


1. Experiment vision: To write a program on the concept of virtual function and virtual
base class.
2. OBJECTIVE: Program to implement virtual function and virtual base class.
3.1 THEORY:
  • Virtual function in C++:
  • When we use same function name in both base class and derived the function in base class is declared as virtual using keyword “virtual” preceeding in normal declaration when the function in C++ determine which function to use of runtime based on type of object pointed to by base pointer i.e by making the base pointer points to different object we can execute different version of virtual function.

3.2 PROGRAM:
1) Following is an example which makes use of virtual function.
#include <iostream>
using namespace std;
class base
{
public:
virtual void show()
{
cout<<"\n Base class show";
}
void display()
{
cout<<"\n Base class display";
}
};
class derive:public base
{
public:
void display()
{
cout<<"\n Drive class display";
}
void show()
{
cout<<"\n Drive class show";
}
};
int main()
{
base obj1;
base *p;
cout<<"\n\t p points to base:\n";
p=&obj1;
p->display();
p->show();
cout<<"\n\t p points to drive:\n";
derive obj2;
p=&obj2;
p->display();
p->show();
return 0;
}


3.3 OUTPUT:
4.1 THEORY:
  • Virtual base class:-
  • Virtual inheritance is a topic of object-oriented programming. It is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes a common direct base for the derived class and any other class that derives from it. In other words, if class A is virtually derived from class V, and class B is derived (directly or indirectly) from A, then V becomes a direct base class of class B and any other class derived from A. The best-known language that implements this feature is C++.
  • This feature is most useful for multiple inheritance, as it makes the virtual base a common subobject for the deriving class and all classes that are derived from it. This can be used to avoid the problem of ambiguous hierarchy composition by clarifying ambiguity over which ancestor class to use, as from the perspective of the deriving class (B in the example above) the virtual base (V) acts as though it were the direct base class of B, not a class derived indirectly through its base (A).It is used when inheritance represents restriction of a set rather than composition of parts. In C++, a base class intended to be common throughout the hierarchy is denoted as virtual with the virtual keyword.
4.2 PROGRAM:
2) Following is an example which makes use of Virtual base class.
#include <iostream>
using namespace std;
class student
{
protected:
int id;
char name[20];
public:
void getdata()
{
cout<<"Enter the name and id\n";
cin>>name>>id;
}
void putdata()
{
cout<<"\nname="<<name<<"\nid="<<id;
}
};
class marks:public virtual student
{
protected:
int sub1,sub2;
public:
void getinfo()
{
cout<<"Enter marks for sub1 and sub2\n";
cin>>sub1>>sub2;
}
void putinfo()
{
cout<<"\nsub1="<<sub1<<"\nsub2="<<sub2;
}
};
class bonus:virtual public student
{
protected:
int b;
public:
void get_b(int x)
{
b=x;
}
void show_b()
{
cout<<"\nbonus="<<b;
}
};
class result:public marks,public bonus
{
int total;
public:
void display()
{
putdata();
putinfo();
show_b();
total=sub1+sub2+b;
cout<<"\ntotal marks="<<total;
}
};
int main()
{
result r;
r.getdata();
r.getinfo();
r.get_b(5);
r.display();
return 0;
}

4.3 OUTPUT:
5. conclusion:
Thus a C++ program Programs by using virtual function and virtual base classhas been executed successfully.
6. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the
experiment, and check the algorithm and program.


7. REMARK:
Function is the concept are used for calling the values from outside the main function & same function call called many times by main().
8. Discussion Questions:
1) What is virtual function?
____________________________________________________________________________________________________________________________________________________________
2) What are rules for virtual function?
____________________________________________________________________________________________________________________________________________________________
3) Why to use virtual function.
____________________________________________________________________________________________________________________________________________________________
4) What do you mean by pure virtual function?
____________________________________________________________________________________________________________________________________________________________
















Date:
Experiment No: 07


1. Experiment vision: To write a program on the concept of Abstract class and Pure
virtual function.
2. OBJECTIVE: Program to implement Abstract class and Pure virtual function.
3.1 THEORY:
  • Abstract class and Pure virtual function :
An abstract class is one that is not used to create objects. An abstract class designed only to act as a base class. It is design concept in program development and provide a base upon which other classes may be built. We declare a function virtual inside the base class and redefine it in the derived classes. The function inside the base class is rarely used for performing any task. It only serves as placeholder. A virtual function equated to zero is called pure virtual function. It is a function declared in a base class that has no definition relative to the base class. A class containing such pure function is called an abstract class.

3.2 PROGRAM:
2) Following is an example which makes use of Pure virtual function and Abstract class.

#include <iostream>
using namespace std;
class base
{
public:
void display( )
{
cout<<"\n Display base";
}
virtual void show()=0;
};
class derived:public base
{
public:
void display( )
{
cout<<"\n Display derived";
}
void show()
{
cout<<"\n show derived";
}
};
int main()
{
derived d;
base *bptr;
bptr= &d;
bptr->display( );
bptr->show( );
return 0;
}


3.3 OUTPUT:


4. conclusion:
Thus a C++ programs by using Abstract class and Pure virtual function has been executed successfully.




5. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the
experiment, and check the algorithm and program.


6. REMARK:
Object of abstract class can not be created.

7. Discussion Questions:
1) What do you abstract class? Write the syntax of it.
____________________________________________________________________________________________________________________________________________________________
2) What is Pure virtual function? Write syntax of it.
____________________________________________________________________________________________________________________________________________________________
3) What do you mean by virtual destructors?
____________________________________________________________________________________________________________________________________________________________
4) What do you mean by dynamic binding and late binding?
____________________________________________________________________________________________________________________________________________________________
5) What do you mean by constructors?
____________________________________________________________________________________________________________________________________________________________


Experiment No: 08


1. Experiment vision: To write a program on the concept of friend Function and this
pointer.
2. OBJECTIVE: Program to implement friend function and this pointer.
3.1 THEORY:
  • Friend Function:

  • The function that are declared with the keyword friend are known as friend functions.
  • A function can be declared as a friend in any number of classes.
  • A friend functions, even though not a member function has full access rights to the private members of class.

3.2 PROGRAM:
2) Following is an example which makes use of friend function.

#include <iostream>
using namespace std;
class sample
{
float x,y;
public:
void read();
friend void mean(sample);
};
void sample::read()
{
cout<<"\nEnter values of x & y:";
cin>>x>>y;
}
void mean(sample z)
{
float r=(z.x+z.y)/2;
cout<<"\nmean value="<<r;
}
int main()
{
sample s;
s.read();
mean(s);
s.read();
mean(s);
return 0;
}


3.3. OUTPUT:
4.1 THEORY:
  • this pointer:

  • C++ uses a unique keyword called this to represent an object that invokes a member function.
  • This is a pointer that points to the object for which this function was called.
  • The pointer this acts as an implicit argument to all the member functions
4.2 PROGRAM:
1) Following is an example which makes use of this pointer.
#include <iostream>
using namespace std;
class sample
{
private:
int x;
public:
void test( )
{
this->x=81;
cout<<this->x;
}
};
int main( )
{
sample s;
s.test( );
return 0;
}
4.3 OUTPUT:
5. conclusion:
Thus a C++ programs by using friend function and this pointer has been executed successfully.
6. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the
experiment, and check the algorithm and program.

7. REMARK:
Friend function accesses the class variable a and b by using the dot operator and the object passed to it. This is a pointer can be treated as pointer to an object and can be used to access the data in the object it points to.

8. Discussion Questions:
1) What do you mean by friend function?
____________________________________________________________________________________________________________________________________________________________
2) Explain the special characteristic of friend function?
____________________________________________________________________________________________________________________________________________________________
3) What do you mean by friend class? Explain with example.
____________________________________________________________________________________________________________________________________________________________
4) What is the purpose of this pointer?
____________________________________________________________________________________________________________________________________________________________

No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.