Class Vs Function

Posts 1–6 of 6 · Page 1 of 1
Class Vs Function
I'm learning c++, and I've learned about classes, but the thing that's bugging me is I'm sure I don't understand them (otherwise I wouldn't need to to ask this question) So can someone please tell me what the advantage of using a class over a function is
Classes are OOP. They hold Variables, Functions, and even other classes, well, pretty much anything can be in a class. THe point of a class is to use it with OOP. Functions just do a set of instructions.
Quote Originally Posted by tdct View Post
I'm learning c++, and I've learned about classes, but the thing that's bugging me is I'm sure I don't understand them (otherwise I wouldn't need to to ask this question) So can someone please tell me what the advantage of using a class over a function is
You cant rly compare them as theyre used to do different things. A function just makes some instructions more compact if you use the same instructions more than one time. A class is used to create objects (instances), for example if you make a game, you could make a class for enemies.
Quote Originally Posted by Nico View Post
You cant rly compare them as theyre used to do different things. A function just makes some instructions more compact if you use the same instructions more than one time. A class is used to create objects (instances), for example if you make a game, you could make a class for enemies.
@Nico this is how it's used on cplusplus (c++ tutorial site)
Code:
// classes example
#include <iostream>
using namespace std;

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b) {
  x = a;
  y = b;
}

int main () {
  CRectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}
Which is why I posted this. I'm reading a book and both times they delved into classes they where similar to that. Maybe I'm just at a point where they are over simplifying everything, but that's why I thought classes and functions are essentially the same.
Quote Originally Posted by tdct View Post


@Nico this is how it's used on cplusplus (c++ tutorial site)
Code:
// classes example
#include <iostream>
using namespace std;

class CRectangle {
    int x, y;
  public:
    void set_values (int,int);
    int area () {return (x*y);}
};

void CRectangle::set_values (int a, int b) {
  x = a;
  y = b;
}

int main () {
  CRectangle rect;
  rect.set_values (3,4);
  cout << "area: " << rect.area();
  return 0;
}
Which is why I posted this. I'm reading a book and both times they delved into classes they where similar to that. Maybe I'm just at a point where they are over simplifying everything, but that's why I thought classes and functions are essentially the same.
In that sense, functions will do.but what if your class has multiple methods, multiple data types, more classes inside(nested classes), structs, or unions?
An example is, A person class, with arms , legs, body, head, foot etc..
Posts 1–6 of 6 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us