Placing Parameters in Constructors - C++ Class

C++ examples for Class:Constructor

Description

Placing Parameters in Constructors

Demo Code

                                                    
#include <iostream>
#include <string> 
using namespace std;
                                                    
class Cat/*w w  w.j  a  v a 2  s.  com*/
{
private:
    string Name;
                                                    
public:
    Cat(string StartName);
    void WhatIsMyName();
};
                                                    
Cat::Cat(string StartName)
{
    cout << "Starting!" << endl;
    Name = StartName;
}
                                                    
void Cat::WhatIsMyName()
{
    cout << "My name is " << Name << endl;
}
                                                    
int main()
{
    Cat *Sam = new Cat("Sam");
    Cat *Sally = new Cat("Sally");
                                                    
    Sam->WhatIsMyName();
    Sally->WhatIsMyName();
                                                    
    delete Sam;
    delete Sally;
                                                    
    return 0;
}

Result


Related Tutorials