Thank you in advance for your assistance. I am trying doing this merely for practice to get a better grasp on constructors and how ti implement them.
so..
1)When I run this code it prints the first constructor, after me just declaring the object, why? - and how do i implement it so that it will only implement the constructor i have when i want it to?
2) how do i access the next constructor?
3) i do not have a destructor , am i wrong to think i would want a destructor only if i have pointers in my private?
#include<iostream>
//practice using constructors and destructors
usingnamespace std;
class practice {
int window;
int door;
public:
practice() {
int window = 0;
int door = 0;
cout << "You have no house" << endl;
}
practice(double b) {
cout << "Error whole numbers not real numbers" << endl;
}
void setWin(int n) {
window = n;
}
int getWin() {
return window;
}
void setDoor(int n) {
door = n;
}
int getDoor() {
return door;
}
/*~practice() {
// not sure how to implement/or what to implement
}*/
};
int main() {
practice home;
//practice();
system("pause");
return 0;
}
A constructor is just like any other function- but c++ implicitly calls the default constructor if you haven't passed any parameters when the object is created. If you want to have a function called at a later time, use a class function like setDoor() instead.
You have two constructors:
1 2 3 4 5 6 7 8
practice() {
int window = 0;
int door = 0;
cout << "You have no house" << endl;
}
practice(double b) {
cout << "Error whole numbers not real numbers" << endl;
}
You would call them like this:
1 2 3 4 5 6
//Constructor 1
practice school;
//Constructor 1 Still
practice gym();
//Constructor 2
practice gym(5.5);
Edit:
am i wrong to think i would want a destructor only if i have pointers in my private?
You can call constructors from other constructors only during the creation phase, not later. If you want to call a different constructor you will have to do it during the construction of a new object. To call a constructor from another constructor I believe you will have to be using a newer standard, and the syntax is something like this:
1) The default constructor (line 10) is run when you instantiate the object on line 35. Constructors are always run when you instantiate an object. That is your opportunity to initialize the object. Constructors are not call when you want. If you want to call a constructor at some other time, you're not understanding constructors correctly.
If you don't want the constructor to do anything. The compiler will supply an empty default constructor automatically if you leave out the default constructor. You don't invoke constructors as you were doing on line 36. Not using the opportunity to initialize variables in a constructor is usually a mistake.
2) Instantiate a practice object with a double argument.
practice h2 (2.1);
3) You would want a destructor when you need to clean up resources, such as dynamic memory allocation. For a simple class with only simple variables, you don't need a destructor.
edit:
okay so i figured out number 2.
That's not quite right. You need an object name. See my example.
actually, that's a function declaration. The function `gym' returns a `practice' object and doesn't receive any parameters.
1 2 3 4 5
practice() {
int window = 0; //these are local variables, not your member variables
int door = 0;
cout << "You have no house" << endl;
} //they die here
#include<iostream>
//practice using constructors and destructors
usingnamespace std;
class practice {
int * window;
int door;
public:
practice() {
window = newint;
}
practice(const practice& n) {
door = n.door;
window = new window(getWindow());
}
//figure out how to do a copy constructor
//- does not work with dynamic memory(pointers)
//"" shallow copy
//"" deep copy
void setDoor(int n) {
door = n;
*window = n+5;
}
int getWindow() {
return *window;
}
int getDoor() {
return door;
}
~practice() {
delete window;
}
};
int main() {
//practice();
practice home;
home.setDoor(3);
cout << home.getDoor() << endl;
cout << home.getWindow() << endl;
practice condo(home);
cout << condo.getDoor() << endl;
cout << condo.getWindow() << endl;
system("pause");
return 0;
}
okay That makes sense ne555, I was thinking of linked lists, but now i see the difference. so when i make the deep copy..i have to either print it within the lifetime of the deep copy or call a print method
But doing just
window= newint;// allocates new int memory in heap
but how would i take windows value from 'n' and store it within window to complete the deep copy?
print is called in your copy constructor, and if print takes an object by value, then your copy constructor is called in the invocation of print leading to an infinite loop of sorts.