C++ Static data member Question

Introduction

Write a program that defines a class with one static data member of type std::string.

Make the data member public.

Define the static data member outside the class.

Change the static data member value from the main() function:

You can use the following code structure.

#include <iostream> 

int main() 
{ 
    //your code here
} 


#include <iostream> 
#include <string> 

class MyClass 
{ 
public: 
    static std::string name; 
}; 

std::string MyClass::name = "John Doe"; 

int main() 
{ 
    std::cout << "Static data member value: " << MyClass::name << '\n'; 
    MyClass::name = "Jane Doe"; 
    std::cout << "Static data member value: " << MyClass::name << '\n'; 
} 



PreviousNext

Related