Special class

Pages: 12
I am having some troubles writing a class.
The class has to be called with 3 parameters, which is used to define a values...
something like this.

class Insurance
{int car_value;
int salary;
int householdincome;
....
};

int main()
{
Insurance bob
bob(1000,20,30)
//The idea with this class is, if i declare it, as i just did, the car_value = //1000
//salary = 20
//householdincome = 30
}

But i don't have any idea on how i should make it?
http://www.cplusplus.com/doc/tutorial/classes/

see the section on constructors and destructors.

edit: why are you calling it a "special class"? I'm not sure what you mean by this.
Last edited on
I would think it should be something like this

#include <iostream>
using namespace std;

class insurance{
int car_value, int salary,int householdincome;
public:
insurance (int,int,int);
{return (int car_value, int salary,int householdincome;);}
};

insurance::insurance(int a, int b,int c) {
car_value = a;
salary = b;
householdincome = c
}

But it won't work?
Last edited on
Just a few syntax corrections

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

class insurance{

	int car_value, salary, householdincome;

	public:
		insurance (int,int,int);

};

insurance::insurance(int a, int b,int c) {
	car_value = a;
	salary = b;
	householdincome = c;
}


But I have no idea what that statement is supposed to do:

{return (int car_value, int salary,int householdincome;);}

I have an document with customer names and informationer about their car value, salary and household income..

I wanted to make a class, which could save those values for that certain instance..
so that the car value and etc. isn't the same for each instances/customers but specifically defined using the parameters for each customer...



But what is:

{return (int car_value, int salary,int householdincome;);}

supposed to do? Do you understand what the comma operator actually does?
Return those values?
Do you understand what the comma operator actually does?
the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type)
-Wiki

but still it should be able to show me the garbage values for the first 2 and, give me the last value..
Without seeing the code you've written to output anything, I can't possibly explain what it is you're seeing it output.
Do you understand what the comma operator actually does?


You're confusing things by asking this i think.
It's just simply completely wrong syntax.
I see that the last one had some syntax errors, and what plexus wrote made a bit more sense... but it won't compile..


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
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <iostream>

using namespace std;

class insurance{

	int car_value, salary, householdincome;

	public:
		insurance (int,int,int);

};

insurance::insurance(int a, int b,int c) {
	car_value = a;
	salary = b;
	householdincome = c;
}


int _tmain(int argc, _TCHAR* argv[])
{
	insurance bob;
	bob(5,10,6);
	return 0;
}


I just want to make multiple instances of the class Insurance, which each has it's own car_value, salary and household income, so i can manipulate with those values using member functions.
Last edited on
Your compiler is probably saying this:

'insurance' : no appropriate default constructor available


Because you have created an insurance object called bob, yet you have told your compiler that when you instantiate an insurance object it needs to expect a three integers (you've told it this on line 13).

however, you're trying to somehow (wrongly) push in these parameters after creation. You're doing this on line 27.

Change your main() to
1
2
3
4
5
6
int main()
{
	insurance bob(5,10,6);

	return 0;
}



p.s.
I just want to make multiple instances of the class Insurance


Just want to point out that an instance of a class is called an object.
Last edited on
I know exactly what you're going through. I couldn't understand classes, at first, either. I LOVE them now (they make life so much easier).

Classes are interesting: they allow you to create objects with properties. They are not functions, so they abide by different rules.

Classes have members:

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
class myclass{

public:

    //constructor with an initializer list
    explicit myclass(const int& arg1, const int& arg2) : memb1(arg1), memb2(arg2) {}

    myclass() //default constructor, with no initializer list.  Can/favorable to have one though
    {
         this->memb1 = 0;
         this->memb2 = 0;
     }

     ~myclass(){} //destructor.

     myclass& operator=(const myclass& arg1)
     {
            this->memb1 = arg1.memb1;
            this->memb2 = arg1.memb2;
            return *this;
     }


     int get_memb1() const
     {
          return this->memb1;
     }
     void set_memb1(const int& arg1)
     {
           this->memb1 = arg1;
     }


private:

    int memb1, memb2;
};


So, there are quite a few things I'm throwing at you.

To start with your current situation, constructors return nothing. Constructors can take arguments, but they can't return anyhting. Constructors only construct the object by executing member functions, or initializing variables.

The first, and most obvious is the new explicit declaration. This basically says to (only) constructors to ONLY take that kind of argument. For example, if you do not define explicit for a constructor that takes a string, you can pass a char* to the constructor, and the conversion will take place. If explicit is defined, though, and you try to pass a char* to a constructor that takes a different argument type, then no conversion will take place. Basically, if you declare a constructor as explicit, you're telling it not to take any other kind of argument than the one you explicitly defined. (passing a float to an int constructor is another example) I have experienced weird things happen when I don't use explicit when I include operators (specifically operator=()). I would highly recommend declaring constructors that take any arguments as explicit. Templating classes can make them more flexible, but that's more advanced, I'm sticking to the basics for now.

The second thing you will notice is the wierd text after the explicitly defined constructor. That is an initializer list, and I have read that it is a performance boost, especially if you're only using that constructor to initialize. It will limit your declarations to calling the contructors directly, but that's good practice anyway (it's faster). The brackets on the end ({}) contain the function itself. Since the initializer list does everything we want it to, we don't put anything in there (you can , though, if you want).

The third thing is the default constructor. I prefer using an initializer list (performace!!!), but for the sake of demonstration I have used the function emplementation for you. You can decide later what you like better. Always include a default constructor when you have variables.

The next thing is the destructor. Again, it's in-line. Since the destructors of the int variables are called when the class is destructed, we don't need to put anything there. We would have to use this, though, if we allocated memory or used pointers. aka: if we used int* memb1 instead of int memb1.

Next is the operator. This one takes a class arg. It's always good to have a copy operator. By definition, a class can always construct using it's own type, so you don't need to define the constructor for it, but the operator is not included by default.

next is the member function get_memb1() const. Notice the const at the end of the declaration. That tells the compiler that the function can not modify any member variables of the class. It's good practice to use the const definition when declaring a function that you don't want modifying member variables.

Also note the this pointer. That pointer points to the instance of the class. If you understand pointers, it should be self-explanitory. this-> will dereference the pointer and call a member at the same time. *this is the equivilant of the class itself (it dereferences the pointer, returning the class).

You'll also notice that I'm passing arguments by using their constant adress. const int&, for example, passes the same instance of that integer. It does not create a new integer. If you were not to declare it const, you would be able to modify the exact same variable that you gave to it, that existswithin the calling function. Const correctness is somthing that is good practice.

If you have any more questions, feel free to ask! :D

I find it helpful to think of classes as a way to combine both data and instructions to use that data. They keep all the messy argument passing inside, so that all you have to do is see the members of it.

aka:

instead of calling:

do_this(_this, that, somthing_else);

you can simply:

why_not.do_this();

If you use the explicit declaration, the compiler will not allow you to "call" the constructor outside of initialization (which is bad practice to begin with). You should either declare the variable public, or create a getter function (a function that returns the vairiables). If you prefer to use a getter, here's how:

define the function:

1
2
3
4
5
6
7
8
9
10
void get_the_vars(int&, int&, int&) const;

//in the .cpp file...

void your_class::get_the_vars(int& one, int& two, int& three) const
{
     one = this->memb1;
     two = this->memb2;
     three = this->memb3;
}


when you call the function, then the variables you passed will be modified by the function.

Edit: +1 mutex
Last edited on
about the last thing you said about this pointer..
Just to clarify things.. Can i use it to get the parameters of my object, so i would be able to cout them, and manipulate them piecewise
@John:

In Object Oriented Programming, objects return values when they are referenced. What I mean by this is that every time you reference an object, you're referencing it's value:

aka: string s = "hi", s2 = "hello";

(operator==() returns a boolean value)
(s == s2) is not true, because "hi" does not equal "hello".

if we have a class called myclass with a string inside called s, then we can do this: cout<< myclass.s because the cout class has an overloaded operator for operator<<(const string&) that can handle it.

this is a pointer to the current instance of a class. It is special, in that it refers specifically to the instance of the class itself. It is the same as using myclass::, but if your class name is absurdly long (like, say, dynamic_memory_class) then it is easier to use the pointer variant. It also ahs the advantage of being able to refer to the address of that instance of the class.

(*this).member
is the same as
this->member
the arrow symbol is for convenience. note, though, that the difference in length is but 1 character... You might prefer read-ability over shorthand.

When we call a member, whether it's through the this pointer, or through the class scope, we make a call that returns that member.

if this->member is a string, then we can perform any operations on it that can be done with a string.

We can take this string and swap the first and last characters:

swap(this->member[0], this->member[(this->member.size() - 1)]);

we can add another char* to the end of it:

this->member += " and how are you?";

and so on.

It doesn't matter what data type member is declared as, because when we derefrence this and call that member, we have direct access to it.

Hope this helps.
Also, Mutexe is wrong.

could you tell me which bit of what i said was wrong please?
Ah, never mind. I must have mis-read, but I could have sworn you used a constructor 'call' outside of the declaration. Typically, the compiler (If I remember correctly) allows this if the definition of the constructor's declaration is not explicit. I could be wrong, though, and either way you are correct in what you said.
Please read posts properly before making incorrect accusations.
I am trying to make an overload operator for multiplication.. for my class here so that if i write
bob(5,10,6) * 2 will it be equal to bob(10,20,12)
Pages: 12