Jump to content

Tutorial-the basics of C++ and game making


Recommended Posts

Char's are much easier for me then strings, and take up less memory, but my teacher was forcing us to use only strings to find single letters.

 

string a;

string b;

string c;

 

cout <<"What word are you using?"<<endl;

cin >> a;

cout <<"Wich letter would you like to replace?"<<endl;

cin >. b;

cout <<"Wich letter would you like to replace it with?"<<endl;

cin >> c;

 

//Now there's a for loop and whatnot then you have to check it using substr because you can use a[1]==b. Wich I got today, but now there's a even harder thing in class but I'll get it eventually

Link to comment
Share on other sites

  • Replies 68
  • Created
  • Last Reply

Top Posters In This Topic

Well, I studied the substring a little more, and it seems that there is a major diffreance between SubString and char. SubString represents a part of an actual String, so if you change the SubString, the String will change as well.

 

If you want to convert a char to a SubString, write this simple class:

 

class CharMadeSubString:public SubString
{
 private:
  String sourceString;
 public:
  CharMadeSubString(char ch)
   {
    sourceString=ch;
    SubString(sourceString,0,1);
   }
};

 

You can put it in an header file if you know how. I didn't tested the code, so if there is a problem, just tell me. It will not act like a normal SubString, but you will be able to use it with functions require SubString.

If you don't understand some of the code, just write it, and use it like a function, like "CharMadeSubString('a')". I will probebly get to it later on, and so will your teacher.

Link to comment
Share on other sites

Here are some speciel operators:

 

The () operator. This operator is declared inside a class like that:

return_type operator ()(arguments)

 

and you use it like this:

object(arguments)

 

The speciel thing about this operator, is that it can have as many arguments as you want.

 

 

The casting operator is used to change the type of the object, declared inside a class like this:

operator type()

 

The speciel thing about this operator, is that you don't need to write the return type before the "operator" keyword. The compiler knows what the return type is according to the operators name.

 

It is called when you try to put the object inside a variable of a different type, or when you call it directly: (type)object);

 

class Cls
{
 private:
  int value;
 public:
  Cls(int v)
   {
    value=v;
   }
  operator int()
   {
    return value;
   }
};

void main()
{
 int integer=Cls(9);
}

Now integer is set to 9. Ofcourse you can put more complex code inside the operator function.

 

 

The intput/output operators, << and >>, are acting just like the normal operators, but they are the most complex ones. That is because the way you use them. As you remember, with a member(inside a class) operator function, the first operand is the object itself, stored in the variable "this". Lets take a look at this simple printing command:

cout<<o;

The first operand is cout!!! That means that if o is an object, the operator function can not be a member of o, and it can not use it's private memeber variables, which we want to print!!!

So how do we solve the problem? There are three ways:

1)We can supply our class with get and set functions. However, that way, the whole world can have access to it's private members, and we don't want that.

2)We can make the operator function a friend of our class. However, if I will teach you that, and my programing teacher will find it our, she will kill me. I will be killed anyways, since I tought here the goto command, and I don't think I can be killed twice. So this is not an option here.

3)We can supply our class with read/write functions. This will be what we do:

 

#include<iostream.h>
class Cls
{
 private:
  int value;
 public:
  Cls(int v)
   {
    value=v;
   }
  void write(ostream& ostrm)
   {
    ostrm<<value;
   }
};

ostream& operator<<(ostream& ostrm,Cls cls)
{
 cls.write(ostream);
 return ostream;
}

void main()
{
 cout<<Cls(5);
}

You can do the same thing with the input operator >>.

 

The reason the operator function returns ostream& is that it will be possible to chain:

cout<<"Cls="<<Cls(5)<<"got that?\n";

 

 

That will be all for today. Class dissmissed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...