Code:
#include <iostream>
#include <string>
using namespace std;
class parent{
private:
string name;
public:
//setter method
void setName(string Name) { name = Name; }
//getter method
string getName() { return name; }
};
int main(){
//declaring object
parent father;
//initializing variables (assigning)
father.setName("John");
//output to screen
cout<< father.getName() <<endl;
}
or i can do it this way!
Code:
#include <iostream>
#include <string>
using namespace std;
class parent{
private:
string name;
public:
//constructor & destructor
parent(string);
~parent();
//getter method
string getName() { return name; }
};
parent::parent(string name){
this -> name = name;
};
parent::~parent(){
//deconstructor
};
int main(){
// declaring object
parent john("John");
//screen output
cout<< john.getName();
}
both ways are basically doing the same thing so which one do i use?