Template Concept

Write a program to create a Student class having three data members std_rollno, std_name, std_marks, along with two methods set_data() method to assign values in data member and display() method to show data on the console. Above Student class used by two different colleges but some change in a datatype of the data member. College 1 used integer std_marks, whereas college 2 used float std_marks. How to handle this problem using the template concept? Assume suitable data wherever required.



The solution to the Above Question-



#include<iostream>
using namespace std;

template <typename T>
class student
{
    private: int std_roll;
             string std_name
             T std_marks;
    public: void set_data(int r,string s,T marks)
            {
                std_roll=r;
                std_name=s;
                std_marks=marks;
            }
            void display()
            {
                cout<<"Roll No: "<<std_roll<<endl;
                cout<<"Name: "<<std_name<<endl;
                cout<<"Marks: "<<std_marks<<endl;
            }
            
};
class college_1public student<int>
{
    
};
class college_2public student<float>
{
    
};
int main()
{
    college_1 A;
    college_2 B;
    A.set_data(1045,"Raunit Verma",98);
    A.display();
    cout<<endl;
    B.set_data(1879,"Verma Raunit",86.5);
    B.display();
    return 0;
}


OutPut

Roll No: 1045 Name: Raunit Verma Marks: 98 Roll No: 1879 Name: Verma Raunit Marks: 86.5

Post a Comment

0 Comments