someboddy Posted December 31, 2004 Author Posted December 31, 2004 Are you talking about a loop that simply run x times?
Lucandrake Posted January 1, 2005 Posted January 1, 2005 The only loops I know are "while" and "for", I want to know a line of coding that makes the program go back to a certian code upon something not being inserted, like, instead of doing while ((b>2) || (b<1)){ cout <<"Enter the number of what you want to do 1=add,2=subtract)"<<endl; cin >> b; if ((b>2) || (b<1)) cout <<"Error"<<endl; } I would like a line of coding that makes me go back to the certain line of coding instead of all that paragraph...
someboddy Posted January 3, 2005 Author Posted January 3, 2005 This command is evil!!! It can ruin the stracture of your program. I will only teach you this command, if you will promise me to use it carefully, only if it it will save you tons of if's, and only after you make sure it will only affact a small and limited section of your program, I will teach you that forbidden command.
Lucandrake Posted January 21, 2005 Posted January 21, 2005 Yes I promise to be carfull with this command, I need it! This class is giving me headaches with 20 if's!!
someboddy Posted January 22, 2005 Author Posted January 22, 2005 Very well. Open your mind to the evil command, and do not let it corrupt your soul. first, you need an identifier. The identifier's name must follow all of the variables names rules:my_identifier: //Piece of code Then, to jump to that identifier, writegoto my_identifier; So your loop will look like this:loop_start: if(!((b>2)||(b<1))) goto loop_end; cout <<"Enter the number of what you want to do 1=add,2=subtract)"<<endl; cin >> b; if ((b>2) || (b<1)) { cout <<"Error"<<endl; goto loop_start; } loop_end: Be carefull.
Lucandrake Posted January 22, 2005 Posted January 22, 2005 and this identifier cout's or returns error messages? The coding just looks longer to me...
someboddy Posted January 23, 2005 Author Posted January 23, 2005 The identifier just tells the compiler where to jump. If you want to print something on hte screen, print it yourself.
someboddy Posted January 26, 2005 Author Posted January 26, 2005 One of the main ideas of classes is that the user (the user is a programer him/herself, but I call him/her a user, because he/she is using the class we write) will not have to deal with the data of our class, only to call it's functions. However, when the user creates an object of our class, it can contain anything. We want a way to initilize this object, to contain usefull data. Lets say we have a Point(classes names start with capital letter by tradition. It makes the program easier to read) class, containing x and y. we want that when the user creates an object of the type point, both x and y will be set to 0. we do it like that:class Point { private: int x,y; public: Point() { x=0; y=0; } /*all the other functions of the class Point. you can put functions before the constractor, but I didn't wanted to say the same thing before the constractor*/ };You are probebly asking yourself, "What's up with that Point function, containg no return type, not even void? Is someboddy crazy"This function is called constractor C-O-N-S-T-R-A-C-T-O-R. Or shortly c'tor. It is called when the object is created in the memory of the computer - when you declare it. It's name is the name of the class, and it has no return type - since it can't return anything. The constractor is also called when you use the "new" command. The constarctor can also have arguments, so the user can set the values of the object when it's created. You do it like that:class Point { private: int x,y; public: Point() { x=0; y=0; } Point(int nx,int ny) { x=nx; y=ny; } //all the other functions of the class Point. };Hey, what's going on? How come two functions have the same name?Those who know JAVA can tell you that this is called function overloading. Many functions can share the same name, as long as they have different argument list, so the compiler will know which function you are calling. You can do it with every function, not just a constractor. This is a C++ feature, so it won't work on C.So, how do you use a constractor with arguments? It's very simple:Point p1;//p1.x is 0,p1.y is 0 Point p2(3,5);//p2.x is 3,p2.y is 5 Point *p3=new Point(4,2)//p3->x is 4,p3->y is 2 You can also use a constractor with arguments to create imidiate values of you class. Just look at the example:Point p1; p1=Point(7,6);"Point(7,6)" creates a point on the run and call the constractor, which sets it's x to 7 and it's y to 6. Then that point is copied to p1, and the temporary point is destroyed. C++ let you chose what to do when an object of you class is destroyed in the end of a function that the object is part of, when the program ends, when you use the "delete" command, or when you use it as an imidiate value, like in the latest example. You do it by declaring a destractor:class Point { private: int x,y; public: Point() { x=0; y=0; } Point(int nx,int ny) { x=nx; y=ny; } ~Point() { cout<<"Bye Bye"; } //all the other functions of the class Point. };As you can see, the destractor is declared just like a constractor, only it has '~' before the class name. You can't declare a destractor with arguments, and ofcourse you can't declare more then one destractor for the same class.The destractor for the class Point is pointless. However, destractors are important for classes containing pointers (Like a string class), which need to clear the memory from the data they used. It is also important for other classes like a class that goes to graphics mode on the constractor - you have to exit the graphics mode, aren't you - or a window class in windows - you need to close the window. That will be all for today. Class dismissed(calling the destractor )
someboddy Posted January 27, 2005 Author Posted January 27, 2005 One important thing about constructors I forgot to tell: If you don't declare any constructor, C++ will declare a default constructor for you. The default ocnsructor has no argument's, and it does nothing. If you declare a consructor with arguments, C++ will not declare the default constructor, so unless you declare an argumentless consructor yourself, you will have to use arguments every time you create an object of that class. therefor, this is illegal:class A { public: A(int a) { /bla bla bla } }; void main() { A a; } However, you can do this:A *a; Because no object is created. Ofcourse you will have to insert the argument when you use the "new" command.
someboddy Posted January 31, 2005 Author Posted January 31, 2005 Regular functions or class functions?
Lucandrake Posted January 31, 2005 Posted January 31, 2005 Idk the difference, right now were learning how to make our own functions, last week we already learned funtions that were preloaded by include cmath
someboddy Posted February 1, 2005 Author Posted February 1, 2005 The variables we declared untill now in the classes belong to the object. That means, that if we have variable 'a' in the class 'A', each object of the type 'A' have an 'a' variable of it's own.However, we can declare variables, and functions, which belong to the class. These are called 'static'. So if we have a static variable 'b' in the class 'A', every object of that class will have the same variable, so if 'c' and 'd' are objects of the class 'A', and we write "c.b=3", 'd.b' will allso be equal to 3. however, if we write "c.a=7", 'd.a' will not be equal to 7, unless it was 7 before. Static functions are functions belong to the class, not the object. Static functions can only access static variables, and call other static functions, unless ofcourse they get an objcet as an argument, use global object, or you declare an object inside the function. They cann't access the unstatic variables if call the unstatic functions of the object they belong to, because they belong to no object. Why would you want to declare a function with such limitations? You will see. So how do you do it? You put the keyword "static" before the type of the variable, or the return type of the function, like this:class A { private: static int count=0; //bla bla bla public: //bla bla bla A() { count++; //bla bla bla } ~A() { count--; //bla bla bla } static int howMuch() { return count; } //bla bla bla }; Do not initilize a static variable in the consturctor, because you don't want then to be initilized every time a new object is created. Instend, initilize them when you declare them.'count' is set to 0 when the program begins. Every time a new 'A' object is created, the constructor is calles and 'count' is increased by one. Every time an 'A' object is destroyed, the destructor is called, and 'count' is decreased by one. Therefor, 'count' is the number of objects of the class 'A' currently exist.You can use static variables and functions like you use regular class functions and variable:A a,b; cout<<a.howMuch();however, you don't always have a class of that type, and it's a waste of computing time and memory to create a new instance of that class just to call the function, and in our case, substruct 1 from the result, because we don't want to count the class we just created.Then what can we do? We can use the syntex "classname::variablename" for variables, or "classname::functionname(argumentlist)" for functions, like this:cout<<A::howMuch(); Nice and simple. That will be all for today. Class dismissed.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now