4 Replies - 2221 Views - Last Post: 09 November 2009 - 08:49 AM Rate Topic: -----

#1 charsi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 09

Stream Insertion Operator

Posted 09 November 2009 - 07:27 AM

hey, just posting what the specifications of the program are...i've got most of the code working and i need assistance to understand a few parts and hopefully code it aswell

Specifications-----------------------------------------------------------
Each individual motion, ( fly, run, swim, crawl, walk, or roll ) is a singleton instance of a concrete
class ( FlyMotion, RunMotion, SwimMotion, CrawlMotion, WalkMotion, RollMotion ) that
inherits from the Motion abstract class.
* Motion is a pure abstract class (also called an interface) with no data and the signature of two abstract
function members:
// return a C-style string representing the concrete motion
virtual char * toString() const = 0;
// return true if key is found within the motion actors
virtual bool contains(const char * key) const = 0;
Motion has a stream insertion operator that prints “- I ” then calls the toString() –abstract- member
function. :wub: :wub:
Page 3 of 4
* The 6 concrete motion instances each contain a set of actors (C-style strings, const char *) that can
perform the motion. These classes implement the Motion interface. Their toString() implementation prints
the verb corresponding to the concrete motion and a trailing space, for example: “roll “, or “swim “.
They must implement the singleton design pattern, so only one instance of a concrete motion can exist at one
time
* A main (driver) controls the overall program logic and the interaction with the user. It maintains and
activates a collection of 6 Motion pointers (which may, if you wish your program to be as OO as possible,
be within another class).
The lists of actors that perform each concrete motion may be either read from file or hard-coded within the
driver as you prefer.
End Specifications----------------------------------------------------------

Okay so i've got the pure abstract class 'motion.h' , the concrete classes "XXXMotion.cpp" and their respective interfaces "XXXMotion.h" (Fly,Run,Walk,Swim,Roll,Crawl) and i've been able to use them as singleton instances in the main driver file.
Each concrete class has a data member actors which is of type set<const char *> and i've successfully been able to add actors(i.e pigeon,goose,lizard,boat,plane,ball,stone etc...they are able to perform the above mentioned motions)...each class has its respective actors which are able to perform that specific motion(Fly,walk etc)
i've also been able to use the bool contains function correctly and return 1 if actor exists in class and 0 if not

I am having trouble..doing "Motion has a stream insertion operator that prints “- I ” then calls the toString() –abstract- member
function"part..i've got functions in my concrete classes that are like this -
const char * XXXMotion::toString()const{ cout << "Fly";};


but i am just not able to do the string insertion operator part as motion.h is supposed to have its signature and its meant to be implemented in the concrete class and also its meant to add " -I" and call the function toString() for that particular Class aka XXXMotion.

any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Stream Insertion Operator

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Stream Insertion Operator

Posted 09 November 2009 - 07:35 AM

by "String insertion operator" what do you mean?

are you looking for something with a syntax like this:

cout << objj; //out puts "I fly"

or

const char action = obj; //action = "I fly"

basically can you are using jargon that I don't understand. perhaps an example of what you need.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Stream Insertion Operator

Posted 09 November 2009 - 07:46 AM

I would guess that he meant "stream insertion operator".

Here's a link I found on google - http://www.cs.niu.ed.../insertion.html
Was This Post Helpful? 0
  • +
  • -

#4 charsi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-November 09

Re: Stream Insertion Operator

Posted 09 November 2009 - 07:57 AM

yeah sorry i meant stream insertion, damm i even got it wrong on the topic name....anyways the link u posted above is somewhat of what i need but the issue im having is...
I have a pure abstract class 'motion.h'
this is implemented in a concrete class, my troubles come up when i declare something like
ostream & operator<<(ostream & osObject, const ClassName & rightOp)

and when i try to implement it as well, sometimes it says it cannot take 2 arguments must take one,..smtimes it says must take two..have no idea whats going on with it..
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Stream Insertion Operator

Posted 09 November 2009 - 08:49 AM

Generally the process is to make a frend function for the ostream & operator<<(ostream &lhs, ThisClass &rhs) (although is all of the required data can be obtained though standard interface no need for it to be a friend function).

#include <iostream>
#include <ostream>
#include <string>

class duck {
	private:
	std::string str;
	
	public:
	duck() {
		str = "Quack";
	}
	friend std::ostream &operator<<(std::ostream &out, duck donald);
};

std::ostream &operator<<(std::ostream &out, duck donald) {
	return (out << "I " << donald.str);
}

int main() {
	duck mallard;
	std::cout << "mallard: " << mallard;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1