Tuesday, 21 April 2015

Write a program that will demonstrate conversion from one class to another class type

Write a program that will demonstrate conversion from one class to another class type. The program that uses the following properties – two class name- studenta, studentb. studentb class contains public member float marks, one constructor function studentb(), one display() to display marks of studentb. studenta class contains public member float marks, one constructor function studenta() that uses to enter marks of studenta, one overloading conversion function operator studentb() which is used to convert the value from class studenta to class studentb, one display() to display marks of studenta. Inside main(), a and b is the object for class studenta and studentb respectively. We will display the value of a first, then we will convert the value from a to b and after that we will display the value of object b. 

#include<iostream>
using namespace std;
class studentb
{
public:
    float marks;
    studentb(){}
    void  display()
            {
                cout<<marks<<endl;
            }
};
class studenta
{
public:
    float marks;
    studenta()
    {
        cout<<"Enter the marks : ";
        cin>>marks;
    }
    operator studentb();
    void display(){cout<<marks<<endl;}
};
studenta::operator studentb()
                {
                    studentb temp;
                    temp.marks=marks;
                    return temp;
                }
int main()
{
    studenta a;
    cout<<"Studenta Marks is : ";
    a.display();
    studentb b;
    b=a;
    cout<<"Studentb Marks is : ";
    b.display();
return 0;
}

No comments:

Post a Comment