C++ Class template Question

Introduction

Write a program that defines a simple class template that has one data member of a generic type, a constructor, a getter function of a generic type, and a setter member function.

Instantiate a class in the main() function for int and double types:

You can use the following code structure.

#include <iostream> 

int main() 
{ 
    //your code here
} 


#include <iostream> 

template <typename T> 
class MyClass 
{ 
private: 
    T x; 
public: 
    MyClass(T xx) 
         : x{ xx } 
    {} 

    T getx() const 
    { 
        return x; 
    } 

    void setx(T ax) 
    { 
        x = ax; 
    } 

}; 

int main() 
{ 
    MyClass<int> o{123}; 
    std::cout << "The value of the data member is: " << o.getx() << '\n'; 
    o.setx(456); 
    std::cout << "The value of the data member is: " << o.getx() << '\n'; 

    MyClass<double> o2{ 4.25 }; 
    std::cout << "The value of the data member is: " << o2.getx() << '\n'; 
    o2.setx(6.28); 
    std::cout << "The value of the data member is: " << o2.getx() << '\n'; 
} 



PreviousNext

Related