7 Replies - 1106 Views - Last Post: 20 January 2010 - 08:50 PM Rate Topic: -----

#1 orca600   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-December 09

Objects/ Classes

Post icon  Posted 20 January 2010 - 02:59 PM

I have been told that classes store data about a certain type of object. And that these are not real-world objects, but the essential definition of real-world objects... Could somone possible explain this concept in a different way as I do not understand?
Is This A Good Question/Topic? 0
  • +

Replies To: Objects/ Classes

#2 4D1   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Objects/ Classes

Posted 20 January 2010 - 03:01 PM

A class is a way of holding information, but unlike other types of data structures it can hold functions aswell.

consider this example:

class Example
{

	  int a;
	  int b;

	  public:
	  void set_value(int, int);
	  int times(){return (a*b);}
};

int Example::set_value(int x, int y)
{
	  a=x;
	  b=y;
}

int main()
{
	  Example ex;
	  ex.setvalue(10,15);
	  cout << ex.times();

}



So we have created a class called Example, this class holds 2 integers and 2 functions, this class can then be used like a data type like int, in the main function we create an instance of Example or an Object of Example we called it ex, ex is now kind of like a variable of type Example, it has the properties of the Example class so we can access the functions and the variables of the class via the object. Classes are just a way to write more modular code.

This post has been edited by 4D1: 20 January 2010 - 03:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 orca600   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-December 09

Re: Objects/ Classes

Posted 20 January 2010 - 03:05 PM

View Post4D1, on 20 Jan, 2010 - 02:01 PM, said:

A class is a way of holding information, but unlike other types of data structures it can hold functions aswell.


I understand that much but what I do not is this talk of 'objects'. Like what is an object in this regard?
Was This Post Helpful? 0
  • +
  • -

#4 4D1   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 225
  • Joined: 22-October 09

Re: Objects/ Classes

Posted 20 January 2010 - 03:24 PM

Think of an object as a variable, and the class as the data type i.e:

int X;

Example ex;

X is a variable of type int, just as ex is an object of type Example.

I suppose looking at your post, an example to explain why someone has explained it to you in that way is:

imagine you had a template for an apple, this template held information about the apples height, circumference, colour and weight, if we created a real object from this template we could call it granny smith and give it the relevant characteristics, we could also create another real object of this template called red and give it appropriate characteristics, both RED and GRANNY SMITH would be objects of the apple family Hopefully that makes sense

This post has been edited by 4D1: 20 January 2010 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Objects/ Classes

Posted 20 January 2010 - 04:25 PM

"Object" means different things in different programming languages, in C++ an object is defined as "something which exists in memory".

The 'thing' in memory can be anything, including the examples already shown in this thread where objects are created from class types, or objects created using simple data types such as int or double.

The important distinction is that objects are more than 'raw data' they have some kind of real world semantics or meaning. e.g. an 'int' is more than a series of 0's and 1s in memory, it represents a whole number (an integer) with a maximum and minimum value, and operations which are defined for it such as addition, subtraction, multiplication, etc.
Was This Post Helpful? 0
  • +
  • -

#6 bodom658   User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Objects/ Classes

Posted 20 January 2010 - 04:37 PM

Simply: A class is a cookie cutter, the object is the cookie.

Think of the class as a template for objects. These objects can differ, having different data, but they show basically the same behavior.

Two fstream objects are sitting in a room. One named ifstream, one named ofstream. Ifstream decided to read something, and ofstream was like "damn, i wish I could do that...." and wrote down what he saw.
Was This Post Helpful? 0
  • +
  • -

#7 orca600   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-December 09

Re: Objects/ Classes

Posted 20 January 2010 - 05:10 PM

View Post4D1, on 20 Jan, 2010 - 02:24 PM, said:

Think of an object as a variable, and the class as the data type i.e:

int X;

Example ex;

X is a variable of type int, just as ex is an object of type Example.

I suppose looking at your post, an example to explain why someone has explained it to you in that way is:

imagine you had a template for an apple, this template held information about the apples height, circumference, colour and weight, if we created a real object from this template we could call it granny smith and give it the relevant characteristics, we could also create another real object of this template called red and give it appropriate characteristics, both RED and GRANNY SMITH would be objects of the apple family Hopefully that makes sense


Yeah, I'm pretty sure I understand now, thanks. I thought that could be what it was meaning by real-world object. My understanding now is that a class serves two main purposes: to be a template to be used more then once and to group relative things about a specific thing, like a paragraph in the english language. I hope this is correct.
Was This Post Helpful? 0
  • +
  • -

#8 Splatocaster   User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Objects/ Classes

Posted 20 January 2010 - 08:50 PM

Think of a class as a general object (such as computer, ball, phone, etc).

Each general object has a bunch of things that can be different from other kinds of the general object. They can have differant sizes, etc.

Let's use the ball example. You can make a class called Ball. Each ball could have a different size, different color, different material. So, the ball class would have a variable for the possible specifications of the ball. The ball can also do things, such as bounce, be thrown, or be caught. Each one of these can be a function inside the class.

In C++, this would be shown as:
class Ball {
		int size, color, material;	   // Each individual ball can have its own size, color, and material
		void bounce();					// A function that will make the individual ball bounce
		void beThrown();				// A function that will make the individual ball be thrown
		void beCaught();				// A function that will make the individual ball be caught
};



You can have many kinds of balls. You can have a tennis ball, a baseball, and a football.

In C++, you can create an object of each class for each type of ball, and set the variables to match:
Ball tennis_ball;						// Create a tennis ball
tennis_ball.size = 3;
tennis_ball.color = yellow;
tennis_ball.material = bouncystuff;

Ball baseball;							// Create a base ball
baseball.size = 4;
baseball.color = white;
baseball.material = hardstuff;

Ball football;							 // Create a football
football.size = 6;
football.color = brown;
football.material = leather;



Hope this helps. Just came up with this analogy as I type.
Was This Post Helpful? 1

Page 1 of 1