Link to home
Start Free TrialLog in
Avatar of b_acs
b_acsFlag for United States of America

asked on

How do you use a pointer to a class object??

I am in my second term of pogramming (nubie).  I am writing a code for a class assignment.  I am supposed to create a class and ask the user to enter stuff in the constructor. Then I am supposed to create a Print function that outputs that stuff.  In main I am supposed to create a group object and a pointer this group object.  My program is supposed to print using both the object name and the pointer name.

My problem is the pointer.....I'm not sure how to do this.   I will post what I have and ask for your guidance again.
// Day7.cpp : Defines the entry point for the console application.
//
 
#include <iostream>
 
using namespace std;
//declaration section
 
class Group
{
public:
	Group();
	void Print();
 
private:
	int NumberInGroup;
	char Name[75];
};
//implementation section
Group::Group()
{
	cout << "Enter the number in the group: ";
	cin >> NumberInGroup;
	cout << "Enter the name of the group: ";
	cin.getline(Name,74, '\n');
}
void Group::Print()
{
	cout << "The group called \"" << Name << "\" has " << NumberInGroup << "members.\n";
}
int main()
{
	Group obj;
	Group *ptr = 0;
 
	obj.Print();
 
	*ptr = obj;
 
	Group *ptr.Print();
 
	
	return 0;
}

Open in new window

Avatar of b_acs
b_acs
Flag of United States of America image

ASKER

Ok I think it should be more like this, but still not right......
// Day7.cpp : Defines the entry point for the console application.
//
 
#include <iostream>
 
using namespace std;
//declaration section
 
class Group
{
public:
	Group();
	void Print();
 
private:
	int NumberInGroup;
	char Name[75];
};
//implementation section
Group::Group()
{
	cout << "Enter the number in the group: ";
	cin >> NumberInGroup;
	cout << "Enter the name of the group: ";
	cin >> Name;
}
void Group::Print()
{
	if(NumberInGroup != 1)
	cout << "The group called \"" << Name << "\" has " << NumberInGroup << " members.\n";
	else
		cout << "The group called \"" << Name << "\" has " << NumberInGroup << " member.\n";
}
int main()
{
	Group obj;
	Group *ptr = 0;
 
	obj.Print();
 
	*ptr = obj;
 
	ptr->Print();
 
	
	return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of b_acs
b_acs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Infinity08
>> I got it!!!

Correct :)