Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ishamael02

Functions and Classes :(

This topic is 5814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First i do no how to define classes. I have never really used them because I dont know HOW to use them. I need some help with wherre I would use them and how to. Second what would i use a class for and how DO i use a class Please help! Ishamael02 Betrayer of Hope Eldar_mage Peter B

Share this post


Link to post
Share on other sites
Advertisement
A class is essentially a user defined data type (with associated operations). Use it when you need a data type that is not built into the language (say, student or snake as opposed to int or float).

(Yes, there is more to classes - inheritance does spring to mind - but I''m attempting to simplify.)

Share this post


Link to post
Share on other sites
I havent had to use them yet. But I could see how they could be usefull in the future. Im assumming their like records in Pascal. If im correct then all it is something that stores a bunch of variables.

Lets say you have a class called "person". Under person you could have the variables Age, Job, Sex and other things like that.

Now that you have the datatype Person, you could create a variable of that datatype. Such as Fred and declare him as a "person" datatype.

Now Fred has all these variables built in. You could acces them like so

Fred.Age = 21;
Fred.Job = "Burger Flipper";
Fred.Sex = "Male";

Thats the understanding I have of them. Of course, Im not too sure, Im still learning myself. I also know theres different levels they could go on like "public" and so on. But I dont know how that works.

Share this post


Link to post
Share on other sites
sum1, what you are describing is a C struct. C++ classes are far more powerful because they also allow you to define the operations you can perform on them (and overload operators to handle these operations). For instance, if you define a class to describe a vector in space, you can also define operations for addition, subtraction, scalar multiplication, dot product, and so on. Furthermore, C++ classes support (and encourage) data encapsulation through the use of access modifiers (private, protected, public), and (very important!) inheritance (which enables you to make use of inheritance-based polymorphism, which is a very wonderful thing to have).

Note that I call them "C structs" and "C++ structs", respectively. You can still have structs in C++, but they are not quite the same thing as in C: In fact, they are the exact same thing as classes, save that they default to public access and public inheritance (as opposed to private in both cases for classes declared with the class keyword, I think). (Personally, I use structs in C++ only when they are C-type structs, but that''s a matter of personal preference.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
By chance I saw this thread, and decided to summerize and add to what has been previously stated. As stated above, classes are a way of defining new (and possibly complicated) data types. Since you know how to define classes, I will assume you know about the difference in private and public members, and that both data and methods(or functions or procedures) may be members.

The first step after defining a class is to instaciate (declare) an object(variable) of the class. This is done just like any other variable declaration. As with the previous examples, if you wanted to declare fred as an object of the class person it would look like this:

person fred;

Optionally, if you have a non-default constructor(that is one that takes parameters) you can instantiate the object like this:

person fred("fred", age); //Example using a non-default constructor

Just in case you don''t know what I mean by constructor, that would be the the member method that is named the same as the class:

class person
{
person(); //default constructor
person(string name, int age); //constructor that takes parameters
};

Now you can use the dot ''.'' to access the members of the object:

fred.age = 18;

if(fred.IsAdult()) cout << fred.name << " is an adult." << endl;

The above example assumes that age and name are public members. If they were private you would need to make member functions that access them(which is what I suggest).

Well, there it is, the basics of using classes. I know it is a bit involved, but I hope it helps. If I missed anything of importance, please say so.

Share this post


Link to post
Share on other sites
anyone out there who has any experiance will now think i am a moron. I have a calcuator I am working on. Boring I know, and it would pobl take you a secod to wip someting up a lot better than this. I want to use classes or funtins to clean this up a bit that is why I asked So I will post the code and ask, with classes and functions how to clean this up! Appreciate the help

//start code
#include <iostream.h>
#include <stdlib.h> //for for system("pause" function
int main()
{
float one, two, three;
char k;
cout << "Welcome To Peter Bretton''s First C++ Program The Calculator!\nIf you find and bugs please notify me immediatly\nCalculator v1.0"< cout << "What kind of problem do you wish to do (m)ultiplycation/(a)ddition/(d)ivision/\n(s)ubtraction): ";
cin >> k;
if (k == ''m'')
{
cout << "Please enter a number to be multiplyed: ";
cin >> one;
cout << "Please enter your second number: ";
cin >> two;
three = one * two;
cout << one << " times " << two << " = " << three << endl;
system("pause");
return 0;
}
if (k == ''a'')
{
cout << "Please enter a number to be added: ";
cin >> one;
cout << "Please enter your second number: ";
cin >> two;
three = one + two;
cout << one << " times " << two << " = " << three << endl;
system("pause");
return 0;
}
if (k == ''d'')
{
cout << "Please enter a number to be divided: ";
cin >> one;
cout << "Please enter your second number: ";
cin >> two;
three = one / two;
cout << one << " divided " << two << " = " << three << endl;
system("pause");
return 0;
}
if (k == ''s'')
{
cout << "Please enter a number to be subtracted: ";
cin >> one;
cout << "Please enter your second number: ";
cin >> two;
three = one - two;
cout << one << " minus " << two << " = " << three << endl;
system("pause");
return 0;
}
}


Help!

Ishamael02
Betrayer of Hope
Eldar_mage
Peter B

Share this post


Link to post
Share on other sites
Try this. This uses functions to make your calculator:

  
#include<iostream>
#include<stdlib.h>


int Add(float x, float y, float z)
{
z = x + y;

std::cout << x << " + " << y << " = " << z << std::endl;

return 0;
};


int Subtract(float x, float y, float z)
{
z = x - y;

std::cout << x << " - " << y << " = " << z << std::endl;

return 0;
};

int Mult(float x, float y, float z)
{
z = x * y;

std::cout << x << " * " << y << " = " << z << std::endl;

return 0;
};

int Div(float x, float y, float z)
{
z = (x / y);

std::cout << x << " / " << y << " = " << z << std::endl;

return 0;
};



int main()
{
main:

float x, y, z;
char function;

std::cout << "Calculator!" << std::endl;

while (function != 0)
{

std:: cout << "Enter a function, and then two numbers! (Enter 0 for function to exit!)" << std::endl;

std::cin >> function;

if (function == '0')
return 0;

std::cin >> x >> y;

switch(function)
{
case '+':Add(x,y,z);
break;

case '-':Subtract(x,y,z);
break;

case '*':Mult(x,y,z);
break;

case '/':Div(x,y,z);
break;

default: std::cout <<"Invalid function! Please try again!" << std::endl;
goto main;
}
};

system("PAUSE");
return 0;

}


~del

[edited by - Del Snd of Thndr on November 20, 2002 11:36:03 AM]

Share this post


Link to post
Share on other sites
The include statements use iostream and stdlib.h. Also, for sake of neatness, you can drop the "main:" and "goto main;" statments. However, if you do this, you might want to replace "goto main;" with "break;"

~del

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!