GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Sep-2003, 07:34
yannoush yannoush is offline
New Member
 
Join Date: Sep 2003
Posts: 5
yannoush is an unknown quantity at this point

need help to define a class in C++


hi

I am learning to program in C++. I have got some difficulties in defining a class. public ans private type is confusing.
anyway

the class I want to define should collect information about integers. she accepts integers through a method add(). at any time she returns the average of the integers, the median (value on the middle of the list, the sum, and the number of integer.

I have thought about define like this:
CPP / C++ / C Code:
class statistics {

private:

	double mean(void)
	vector<int> mode(void)
	double median(void)
	int sum(void)
	int count(void)

public:
	void add(int t)

};

and for yhe method I have thought about:
CPP / C++ / C Code:
void
statistics::count (void)
{
	ifstream list("list.txt")
	container c;
	int n;
	a=0;
	while (list >> n)
		{
		c.push_back(n);
		a=a++;
		}
	c.sort();
	cout << a << endl;
}




void
statistics::sum (void)
{
	ifstream list("list.txt")
	container c;
	int n;
	sum=0;
	while (list >> n)
		c.push_back(n);
	for (container::iterator i = c.begin(); i != c.end(); i++)
		sum = addsum(*i);
	
	cout << "the sum is " << sum << endl;
}



void
statistics::mean (void)
{
	ifstream list("list.txt")
	container c;
	int n;
	mean=0;
	a=0;
	while (list >> n)
		{
		c.push_back(n);
a=a++;
		}
	for (container::iterator i = c.begin(); i != c.end(); i++)
		sum = addsum(*i);
	
	mean=sum/a;
	cout << "the mean is " << mean << endl;
}
I don't think it is working because I don't think I need to read the file "list"
if someone can give me hints about that, that would be nice
thank you for any help

see ya
  #2  
Old 04-Sep-2003, 10:47
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Hey and welcome to the boards!

OK, your class definition looks okay except for a syntax thing. You're missing the semicolons at the end of each function definition. So it should be like this:
CPP / C++ / C Code:
class statistics {

  public:
     void add(int t);

  private:
     double mean(void);
     vector<int> mode(void);
     double median(void);
     int sum(void);
     int count(void);
};

Next thing I will go into is the public & private properties of objects.

The greatest advantage of Object Orientated programming, is the way we create different objects to handle small jobs, and get them to work together to achieve a larger goal.

Before, we had pages upon pages of functions and structures, which all interacted with eachother. But it was very easy to get mixed up, 2 functions might accidentally use the same variable name - causing unpredictable errors & a very hard bug to find.

There was no way to reserve variables for various "sections" - or objects - which should be exclusive for that function, and not accessible anywhere else.

So objects solve this problem, but how? Public and Private (and Protected - but you won't see this for a bit) properties do this. An object is a collection of variables and functions which interact with eachother only, with some functions defined as public so that the object can be useful.

I'll take your project for example. Your Statistics object will have 5 functions that you will want to be able to call, send data to & retrieve the answers - all from outside the object. Namely: mean(), mode(), median(), count() and add().

These functions must be made public, otherwise C++ will not allow you to call these functions outside the object.

But your Statistics object will also contain a vector, an array of integers which will store the numbers that are added through add(). You don't want other objects to be able to access this - what if something goes crazy and tries to put a character here - not using the add function? If the vector is private, then nothing but the functions in the Statistics object will be able to access it.

So you've to modify your code to do this. I'll leave this bit to you!

Hope this helps
Garth Farley
  #3  
Old 06-Sep-2003, 11:20
yannoush yannoush is offline
New Member
 
Join Date: Sep 2003
Posts: 5
yannoush is an unknown quantity at this point
I understand better the difference public and private, and the fact that the array created by the function add will be only accessible by the other functions: mean, sum...

I try to understand the ifstream and its use:
what I understand is with ifstream I can access a file, which contains the list of integers. the problem is that in my main file I will call the function by somethign like:
list.count
so I think writting ifstream list("list.txt") in the definition of the function is not really accurate. I know that I have to read the integers from a file ".txt", then I have to put them one by one in a container.

I think in the main program I should have
CPP / C++ / C Code:
void main()
{
ifstream list("list.txt");
container c;
....
}
then the definition of the function should become
CPP / C++ / C Code:
void
statistics::sum (void)
{
for (container::iterator i = c.begin(); i != c.end(); i++)
sum = addsum(*i);
cout << "the sum is " << sum << endl;
}


if I do something like that do I have to call "c.sum" c:container?
is that better to read the file ".txt" in each function?

thanks for your help.
yann
  #4  
Old 07-Sep-2003, 08:06
yannoush yannoush is offline
New Member
 
Join Date: Sep 2003
Posts: 5
yannoush is an unknown quantity at this point
hi

I thought about the class definition:

the function class is actually in private, but this function should take the integer list from outside, isn't it?
so I think it is better to create a container which will be private type and the function add will be in public to be able to access to the list.

thanks for help
yann
  #5  
Old 07-Sep-2003, 10:38
yannoush yannoush is offline
New Member
 
Join Date: Sep 2003
Posts: 5
yannoush is an unknown quantity at this point
I thought of the definition:
in private: a vector of integer, which will contain the list
and the function in public.
in each function, I would like to call the vector list, but I don't know if the structure is correct. besides I would like to call the function sum and count in other function. is that possible?
CPP / C++ / C Code:
class statistics {


private:
	vector<int> list;


public:
	
	void add(int t)
		{
		list.push_back(t)
		}
	int sum(void)
		{
		count= list.size();
		for (i=0; i=count; i++)
			sum=sum+list[i];
		cout << "the sum of the integers is: "<< sum << endl; 
		}
		;
	int count(void)
		{
		count= list.size();
		cout << "there is "<< count <<" integer in the list"<<endl;
		};
	double mean(void)
		{
		count= list.size();
		for (i=0; i=count; i++)
			sum=sum+list[i];
		mean=sum/count;
		cout << "the mean value is "<< mean <<endl;
		};
	vector<int> mode(void)
		{
		//number whihc are present twice
		};

	double median(void)
		{
		count= list.size();
		a=count%2;    
		if (a=0)
			{
			b=count/2;
			median=(list[b]+list[b+1])/2;
			}
		else
			{
			b=(count/2)+1;
			median=list[b]; 
		};

};

thanks for all
yann
  #6  
Old 08-Sep-2003, 04:36
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
First, you should use ifstream inside the object. Send the object the name of the file in a method - let the object itself have sole access to the file.

Inside the class definition, you can have a private ifstream variable. Then all the objects methods can read from the file.

So, for example
CPP / C++ / C Code:
class Readme{
public:
    bool setfile(string);
    void printout();

private:
    ifstream file_h;
};

bool Readme::setfile(string filename){
    file_h.open(filename.c_str());
    //have error checking here.
    return true;
}

void Readme::printout(){
    string buffer;
    file_h.getline(buffer);  //read a line from the file
    cout << buffer;
}

You see, you can manipulate the file withiin the object, and no other object can access the file!


For your seconf question, yes you can call all the functions that are in the object, if you are in the object. A member function has access to everything in the object.

Hope this helps,
GF
  #7  
Old 08-Sep-2003, 11:00
comp5018 comp5018 is offline
New Member
 
Join Date: Sep 2003
Posts: 1
comp5018 is an unknown quantity at this point
Hey Yannoush,

Are you enrolled in COMP5018?
  #8  
Old 09-Sep-2003, 01:28
yannoush yannoush is offline
New Member
 
Join Date: Sep 2003
Posts: 5
yannoush is an unknown quantity at this point
yes.

you too?
C programming is not easy, I am doing better and better (I hope!!)
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Keeping track of a private variable outside the class viperv80 C++ Forum 1 03-Dec-2003 18:25
using class CFont aidanh C++ Forum 0 11-Nov-2003 09:01
Upgrade of BBCode class sometime today JdS GIDForums™ 4 03-Nov-2003 08:18
problem with creating class mohammed C++ Forum 1 11-Oct-2003 10:04
[class] Timer JdS PHP Code Library 2 29-Apr-2003 13:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:24.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.