?
Solved

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

Posted on 2008-11-13
3
Medium Priority
?
248 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:b_acs
  • 2
3 Comments
 

Author Comment

by:b_acs
ID: 22956711
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

0
 

Accepted Solution

by:
b_acs earned 0 total points
ID: 22956745
I got it!!!
int main()
{
	Group obj;
	Group *ptr;
 
	obj.Print();
 
	ptr = &obj;
 
	ptr->Print();
 	
	return 0;
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22958012
>> I got it!!!

Correct :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question