Muresha Muresha - 1 year ago 65
C++ Question

Object pointer doesn't work

My task was to create an object in class, initialize it and output(using pointer to class). This code compiles perfectly, but the output doesn't appear. I would really appreciate any help, thank you in advance!

#include <iostream>
#include <string>

using namespace std;

class family
void setWife(string w)
{w = wife;};
string getWife()
{return wife;};
void setHusband(string h)
{husband = h;};
string getHusband()
{return husband;};
void setSon(string s)
{s = son;};
string getSon()
{return son;};
void setDaughter1(string d1)
{d1 = daughter1;};
string getDaughter1()
{return daughter1;};
void setDaughter2(string d2)
{daughter2 = d2;};
string getDaughter2()
{return daughter2;};
double* getPointer()
{return &pointer;};
void initialize()
setWife("Shirley Collin");
setHusband("Donald Collin");
setSon("Collin Collin");
setDaughter1("Harriet Collin");
setDaughter2("Hillary Collin");
friend void output(family* Collin);

string wife;
string husband;
string son;
string daughter1;
string daughter2;
double pointer;

void output(family* Collin)
{cout << "Husband is " <<Collin->getHusband()<< endl;
cout << "wife is " << Collin ->getWife() << endl;
cout << "son is " << Collin->getSon() << endl;
cout << "daughter1 is " << Collin->getDaughter1() << endl;
cout << "daughter2 is " << Collin->getDaughter2()<< endl;

int main()
{family Collin;
family *pointer = new family;
output (pointer);


Answer Source
family Collin;

This constructs an instance of the family class, and initializes it with the values defined in the initialize() method.

family *pointer = new family;
output (pointer);

This constructs a second instance of the family class, does not initialize it in any way, and calls the output() method, to display the contents of the completely uninitialized second instance of this family class.

This is why this program produces no useful output.

You probably want to replace these four lines with:

family *pointer=new family;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download