Can someone please explain this to me!

This Paragraph:

"The notion of class was invented by an Englishman to keep the general population happy. It derives from the theory that people who know their place and function in society are much more secure and comfortable in life than those who do not. The famous Dane, Bjarne Stroustrup, who invented C++, undoubtedly acquired a deep knowledge of class concepts while at Cambridge University in England
and appropriated the idea very successfully for use in his new language.

Class in C++ is similar to the English concept, in that each class usually has a very precise role and a permitted set of actions. However, it differs from the English idea because class in C++ has largely socialist overtones, concentrating on the importance of working classes. Indeed, in some ways it is the reverse of the English ideal because working classes in C++ often live on the backs of classes that do nothing at all."

What englishman and what concept? o.O

Also how do operations on classes work like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CBox
{
public:
double m_Length;
double m_Width;
double m_Height;
};



CBox box1;
CBox box2;
if(box1 > box2) // Fill the larger box
box1.fill();
else
box2.fill();
Last edited on
Well those ops will not work.

Line 13, you must overload the > operator.
Line 14,16, there are no defined methods for fill().

If you need more explanation just say so :)
What does it mean overload?

I know there are no defined methods for fill().

Its just a example code.
Overload - to use same name in different (but hopefully similar) contexts. For example:
1
2
float sqrt( float );
double sqrt( double );

C does not allow overloading, and therefore you would need two names, e.g. sqrtf and sqrtd.

There are already operator> for basic types. Supplying one for CBox too means overloading.
There are two possibilities:
1
2
bool CBox::operator> ( const CBox & rhs )
bool operator> ( const CBox & lhs, const CBox & rhs )

The first is a member function of CBox and as such has direct access to private and protected variables of the CBox.
The second is a separate binary operator function and has to operate with public interface of CBox.
I know what overload means but I just didnt understand how it applied here. Thank you!
Also can someone please explain this englishman crap or is it just a story to help me learn or something?

Thank You!
As relates to your reading, tell your instructor that whoever wrote it has no idea what he is talking about. If you found it elsewhere, you can safely ignore it. (And probably anything else the author says. People with big egos like to run their mouths on stuff they know nothing about.)

The idea of a class in computer programming predates Bjarne Stroustrup, and it is more closely related to mathematics than social theory.

Besides, all that additional baggage about social stuff is crap. Remember, saying a thing is so does not make it so. In the apocryphal words of Freud, "Sometimes a cigar is just a cigar."

(BTW. Freud didn't actually say that... http://quoteinvestigator.com/2011/08/12/just-a-cigar/ )

Hope this helps.
Ok so I can safely ignore that?
Yes. The whole social class thing is nonsense.

No one Englishman invented 'class'. Class distinctions have always existed in societies. Some people simply have more power and wealth than others, and those who have it like to keep it that way.

The comfortable/happy theory comes from the pride of powerful men to justify themselves as they extended their rule over the less fortunate.

The idea that Mr Stroustrup "undoubtedly" appropriated "class" concepts while in England is pure bolony, and to state it as fact is hubris.

So that then is the author's (false) premise, which reads 'Stroustrup went to England and got an understanding of the "class" concept, which he then ported from societal contexts to his programming language.'

The second paragraph is an obvious attempt by the author to understand his premise, because it confuses him. We see it in the very structure, which reads 'Class in C++ is like social classes, except it isn't, sometimes it is the opposite.' He then throws in some doubletalk about how the proletariat both does and does not live off the backs of their superiors.

Perhaps the author is confused by his premise because it makes no sense. The problem is that after reviewing it he has not considered (or refused to concede) that it is nonsense, and cannot get his head around some way to make it sensible.

The truth is much easier to take. Bjarne Stroustrup has never had any difficulty explaining where C++ came from. You can begin reading here:
http://www.stroustrup.com/bs_faq.html#invention
(Make sure to click on some of the links he gives as well -- they go into pretty great detail about what influenced C++'s design.)

The idea of "classes" and "object-oriented program" existed long before Stroustrup came along.

And none of the social class stuff helps you understand C++ any better. All it is is extra junk to jumble around in your brain. Learning to program is hard enough without the extra baggage.

Hope this helps.
Ok thankyou!

Just one last question. Why in the following code does box1 only occupy 24 bits even though it has a function in it?

Where is the function stored?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using std::cout;
using std::endl;

class CBox                             // Class definition at global scope
{
  public:
    double m_Length;                   // Length of a box in inches
    double m_Width;                    // Width of a box in inches
    double m_Height;                   // Height of a box in inches

    // Function to calculate the volume of a box
    double Volume()
    {
      return m_Length*m_Width*m_Height;
    }
};

int main()
{
  CBox box1;                           // Declare box1 of type CBox
  CBox box2;                           // Declare box2 of type CBox

  double boxVolume(0.0);               // Stores the volume of a box

  box1.m_Height = 18.0;                // Define the values
  box1.m_Length = 78.0;                // of the members of
  box1.m_Width = 24.0;                 // the object box1

  box2.m_Height = box1.m_Height - 10;  // Define box2
  box2.m_Length = box1.m_Length/2.0;   // members in
  box2.m_Width = 0.25*box1.m_Length;   // terms of box1

  boxVolume = box1.Volume();           // Calculate volume of box1
  cout << endl
       << "Volume of box1 = " << boxVolume;

  cout << endl
       << "Volume of box2 = "
       << box2.Volume();

  cout << endl
       << "A CBox object occupies "
       << sizeof box1 << " bytes.";

  cout << endl;
  return 0;
}


Output:

Volume of box1 = 33696
Volume of box2 = 6084
A CBox object occupies 24 bytes.


And also why exactly are class members private by default?
Last edited on
In most architectures executable code and program data are stored separately.

Remember that a class like yours (without a vtable) does not need extra space to store any information about the function. All it needs space for is the data.

It is actually identical to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* Here is C code that demonstrates a "class" */
#include <stdio.h>

typedef struct CBox
{
  double m_Length;
  double m_Width;
  double m_Height;
} CBox;

double CBox_Volume( CBox* box )
{
  return box->m_Length * box->m_Width * box->m_Height;
}

int main()
{
  CBox box1;
  CBox box2;

  double boxVolume = 0.0;

  box1.m_Height = 18.0;
  box1.m_Length = 78.0;
  box1.m_Width  = 24.0;

  box2.m_Height = box1.m_Height - 10;
  box2.m_Length = box1.m_Length / 2.0;
  box2.m_Width  = 0.25 * box1.m_Length;

  boxVolume = CBox_Volume( &box1 );
  printf( "\n%s%g", "Volume of box1 = ", boxVolume );

  printf( "\n%s%g", "Volume of box2 = ", CBox_Volume( &box2 ) );

  printf( "\n%s%u%s\n", "A CBox object occupies ", sizeof box1, " bytes." );

  return 0;
}

The C++ way is significantly easier to look at and use.

As soon as you start using polymorphism the advantages become significant.


And also why exactly are class members private by default?

Hey, that's one more than one last question there.

Hope this helps.

I don't know why they are private by default. Does it matter?
So the function is just executable code and not program data?

Sorry about posting another thread. sorry.
Last edited on
Exactly.
Awesome thankyou!
And also why exactly are class members private by default?

Because encapsulation of implementation details is one of the fundamental principles of OO methodology.

In short: the rest of the application should not need to know about the inner workings of the class. That is kept private. The class should provide a clear interface, through its public methods, that allow the rest of the code to use the class for whatever purpose it was written to provide.

One benefit of this is that you are free to modify the inner workings (e.g. data members) of the class if you want to, without needing to go and change lots of places in the code where the class is used - because the interface should remain more or less the same.

Any introductory text on OO programming - or on C++ - should explain this in more detail.
Oo ok thanks! :D Although I am still a bit confused, I believe after some experience I should be able to understand this more.
Last edited on
I would argue that class is private by default because struct is public by default, and "they" just wanted to make us remember one more thing... It's all a conspiracy!

What if payphones are disappearing to make it harder for us to escape from the matrix?
Topic archived. No new replies allowed.