C++ Constructor with Parameters

Description

C++ Constructor with Parameters

 
#include <iostream>
 
using namespace std;
 
class Cat/*from  w ww .  j  a  va  2  s.  co  m*/
{
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;
}



PreviousNext

Related