Sorry to bug you all but I need some advise

I'm not sure if it's me but evertime I step away from c++ I keep forgetting, or worse going backwards but I'm still trying to stick at it, (even tho people say stay away from it.)


In the code below I was trying to make some attempt to work on something and body parts came to mine yet body parts consist of location, assignment and other complected things but I have decided to keep it simple by creating a object as default, then refer to it as a template or default class once all parts are removed while destroying them and recreating them but I think it's all wrong.
(No this isn't homework.)


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 #include <iostream>
#include <string>
#include <iomanip>

using namespace std;
/* Goal: To implement body parts as if it was a processing plant, not using std:: right now.
*/
class Character{
private: int Default_Head; int *Head; string name; 
       
public:
void store_bodyParts( int newHead);
int test_bodyParts( int newHead);
void display_body_Parts();
Character();

  	
 };	
/*
 okay i see a problem now with the default class, every other calls 
carrying over the defualt values, 
so i might just ignore the constructor for now and maybe create inheritance or something later.*/

  // Default value
  Character::Character(){
  name = ("Default constructor ");
  Default_Head = 0;
  
  	};
  	
 // creating a new value
  void Character::store_bodyParts(int newHead)
 {
  //Assigning new memory from head to newHead, this isn't how you assign memory right? 
  Head = new int;
 *Head = newHead;
  newHead = 5;
  cout<<"size of newHead is "<<sizeof(newHead)<<endl;
  cout<<"newHeads value is "<<newHead<<endl;
 
  };
 	
 // Comparing a new value newHead as new memory
  int Character::test_bodyParts(int newHead)
 	{
  //newHead should be greater then 2 when creating a newHead
  if(newHead > 2){
  	cout<<"true"<<endl;
  	}else{
  		cout<<"false, newHead not greater then 2"<<endl;
  		}
  		return newHead;
 	 }
  // displaying output
  void Character::display_body_Parts(){
  cout<<Head<<name<<Default_Head<<endl;
 			}
 			
 	 
  // clean memory and create a new value from a constructor
  
int main()
{
  
  Character Components;
  Components.store_bodyParts(0);
  
  Character Component;
  Components.test_bodyParts(0);
  
  Character Component_display;
  Component_display.display_body_Parts();
  
 

	return 0;
}
Last edited on
Your main program's logic is analogous to this:
1
2
3
4
5
6
7
8
9
10
11
12
#inctude <iostream>
int main()
{
  int x;
  x = 7;

  int y;
  std::cout <<  42 << '\n';

 int z;
 std::cout << z;
}

Does it make sense?
Last edited on
Line 23: In your default constructor, you don't initialize Head. Therefore, the pointer contains garbage.

Line 29: You ignore any previous value of Head. What if you call store_bodyParts() more than once? You will have a memory leak.

Line 50: Head is a possibly uninitialized pointer. You're going to cout the contents of the pointer. Not what it points to.

Line 61,64,67: You're creating three separated unrelated instances. You don't make clear what you're trying to do here, but presumably you want to call all three functions on the same object.
1
2
3
4
5
  Character Components;

  Components.store_bodyParts(0);
  Components.test_bodyParts(0);
  Components.display_body_Parts();

Last edited on
Oh sorry i was kinda busy with work, oh just coming backs making rethink what i did.

Okay well what i was trying to do was:

1 Create something from a private object

2 assigns new memory, to the new object (then delete only when a true condition is met)

3 Display if the body part is missing(or some feedback display)

4 And call a constructor with default settings that can be copied but having problems with 2

( The default constructor i wrote in to see if i can compare it via true or false condition, sadly the test body part condition kept failing as if newHead had 2-4 bytes resulting in true, rather newHead equaling 5, that should bring back false, but i think creating memory will not allow me to set a condition on new memory type; well for me any hows.

In the main i had to declare something.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main()
{
  int x;
  x = 7;

  int y;
  std::cout <<  42 << '\n';

 int z;
 std::cout << z;
return 0;
}


Sorry but i don't understand what that has to do with the code listed above? ^(^-^)


1
2
3
4
5
6
7
8
9
10
11
12
 

You mean i'm doing this?
Character Component_One;

  Component_One.store_bodyParts(0);

Character Component_Two;
  Component_Two.test_bodyParts(0);

Character Component_Three;
  Component_Three.display_body_Parts(); 
Last edited on
You mean i'm doing this?

Yes.

We both do create three unrelated objects. You have three Character objects. I have three integers.

We both change the value of the first object:
You do it with Character::store_bodyParts(), I with assignment operator.

We both print something:
Your Character::test_bodyParts() does nothing with the object (Component_Two) and simply prints "false ..."
I print "42".

We both print "value" of the third object.


If you think (like I do) that my code makes no sense, and my code has same fundamental logic as yours (I assure that it does), then what that says about your code?


creating memory will not allow me to set a condition on new memory type

I wish I'd understand what you mean with that.

If we can get into same page, then we can make progress.
Last edited on
Topic archived. No new replies allowed.