C++ Class Constructor Initializer List Question

Introduction

Write a program that defines a class called MyClass, which has two private data members of type int and double.

Outside the class, define a user-provided constructor accepting two parameters.

The constructor initializes both data members with arguments using the initializer.

Outside the class, define a function called printdata() which prints the values of both data members.

You can use the following code structure.

#include <iostream> 

int main() 
{ 
    //your code here
} 


#include <iostream> 

class MyClass 
{ 
private: 
    int x; 
    double d; 
public: 
    MyClass(int xx, double dd); 
    void printdata(); 
}; 

MyClass::MyClass(int xx, double dd) 
     : x{ xx }, d{ dd } 
{ 

} 
void MyClass::printdata() 
{ 
     std::cout << " The value of x: " << x << ", the value of d: "   
     << d << '\n'; 
} 

int main() 
{ 
    MyClass o{ 123, 456.789 }; 
    o.printdata(); 
} 



PreviousNext

Related