+ 1
please help me, i can't understand each line of this program and it's meaning why we use it? . please explain it.
#include <iostream> using namespace std; class myClass { public: myClass(string nm) { setName(nm); } void setName(string x) { name = x; } string getName() { return name; } private: string name; }; int main() { myClass ob1("David"); myClass ob2("Amy"); cout << ob1.getName(); }
3 Answers
+ 1
You are defining a class myClass
3 public methods
a private name
+ 2
into main you are defining 2 objects, ob1 and ob2
at the end you show Name of ob1 object
+ 1
In this program, you define a constructor with the a parameter and call the setName function with pass a object.
second is, you make a setName function with a parameter and get the value of setName function using getName function.
third is, you call the function and constructor is call only defining a object of a class.