2 Replies - 790 Views - Last Post: 18 February 2009 - 07:33 PM Rate Topic: -----

#1 sputnick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-February 09

increment the value of x by any integer, through a function

Post icon  Posted 18 February 2009 - 07:27 PM

hey guys im pretty new to this site and programming. im working on a homework problem(hence the random class names and stuff) and im having some difficulty with part of it. the code compiles and all but i need to add a friend function that increments the value of x by any integer. anything i try i get an error. as far as i know it has to go in the public part of jealous..maybe not..?

#include <iostream>
using namespace std;

class Jealous {
private:
	int x;
protected:
	static int y;
public:
	Jealous(void) {cout << "Is it mMarch yet?\n"; x=0;}
	Jealous(int i) {setData (i);}
	void setData(int i) {x = i;}
	static void setY(int yy) {y = yy;}
	int getX(void) const {return x;}
	static int getY(void) {return y;}
	
};
class How : public Jealous {
public:
	int getYY(void) const {return y;}
	How(int i) : Jealous(i) {y = i;}
};
int Jealous::y;



void main(void)
{
	cout << Jealous::getY() << endl;
	Jealous guy(11), over; How you(15);
	cout << guy.getX() << "\t" << guy.getY() << "\t" << you.getYY() << endl;
	Jealous::setY(65);
	cout << over.getX() << "\t" << guy.getY() << "\t" << you.getYY() << endl;
}//end of main




Is This A Good Question/Topic? 0
  • +

Replies To: increment the value of x by any integer, through a function

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: increment the value of x by any integer, through a function

Posted 18 February 2009 - 07:31 PM


#include <iostream>
using namespace std;

class Jealous {
private:
	int x;
protected:
	static int y;
public:
	Jealous(void) {cout << "Is it mMarch yet?\n"; x=0;}
	Jealous(int i) {setData (i);}
	void setData(int i) {x = i;}
	static void setY(int yy) {y = yy;}
	int getX(void) const {return x;}
	static int getY(void) {return y;}
//*************************Here*****************************
	friend void incrementX(int n)		   { x +=n; };
   
};
class How : public Jealous {
public:
	int getYY(void) const {return y;}
	How(int i) : Jealous(i) {y = i;}
};
int Jealous::y;



void main(void)
{
	cout << Jealous::getY() << endl;
	Jealous guy(11), over; How you(15);
	cout << guy.getX() << "\t" << guy.getY() << "\t" << you.getYY() << endl;
	Jealous::setY(65);
	cout << over.getX() << "\t" << guy.getY() << "\t" << you.getYY() << endl;
}//end of main




I haven't compiled it. Is this what you're looking for?
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6630
  • View blog
  • Posts: 30,893
  • Joined: 10-May 07

Re: increment the value of x by any integer, through a function

Posted 18 February 2009 - 07:33 PM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1