Inheritance Problem!!!

Hello Guys!!!
I am trying to create the two classes (Base Class & Derived Class) but my AddMember() function is creating problem for me.....I Think :D Here's the code:
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
class User
{
      protected:
      int ID;
      char *Name;
      public:
      User(){};
      User(int,char *){
      int *ID=new int[7];
      char *Name=new char[15];
      }
      void setID()
      {
           cout<<"Enter ID: ";
           cin>>ID;
           int *ID=new int[7];
           }
      void setName()
      {
           cout<<"Enter Name: ";
           cin>>Name;
           char *Name=new char[15];
           }
      int getID()
      {
           return ID;
           }
      char *getName()
      {
           return Name;
           }
      ~User(){
      }
};
class Members: public User
{
      char *Password;
      
      public:
      Members(){};
      Members(int,char *,char *){
      char *Password=new char[8];
      }
      void setPassword()
      {
           cout<<"Enter Password: ";
           cin>>Password;
           char *Password=new char[8];
      }
      char *getPassword()
      {
           return Password;
           }
      void AddMember()
      {
           cout<<"Adding new member information! Please wait...."<<endl;
           setID();
           setName();
           setPassword(); //Program crashes on this step
           }
      ~Members(){
      }
};
int main()
{
    Members obj1;
    obj1.AddMember();
    system ("PAUSE");
    getch();
}

This Program compiles with no errors but when AddMember() function try to ask for Password (line 59) the program crashes. I don't know why this happened everything compiled successfully but now hell program crashes. Anybody can detect the problem? Thanks!!!
Last edited on
1
2
3
4
5
6
void setName()
      {
           cout<<"Enter Name: ";
           cin>>Name;
           char *Name=new char[15];
           }


No comment. PS: There is little point to a set method if the method does the work of a retrieving a value itself (same for the rest of the class). Also, c strings are evil. (so are system and C arrays in general, but w/e).

A set method should just take a value and set a member. Maybe check that value for correctness, but other than that that's it.
In line 37 you declare a variable:
char *Password;

You declare and initialize the same variable (Password) at the same time at line 42:
(line 42) char *Password=new char[8];
I think that shouldn't even compile. Surely it's wrong. Use constructor to initialize your variables. Only for const variables you can initialize when declared.

You have done the same with your base class declaration.

Also in line 48 you declare a new varibale with same name (local one that is). Is this what you wnated to do?
char *Password=new char[8];

Take into mind that line 47 stores the value entered to the some of the other variables. Not the local one. Not that it matters.

Each time you allocate a variable to the stack (with new) you MUST deallocate it using delete at some point (normally destructor). Otherwise there are memory leaks. That simple
Hai,

I have to tried to execute the above code in my Solaris 10 system.
I didn't receive any prompt for typing password, but i could able to provide ID and Name as cin.

hassaanid2011, do you have the same problem or your executable is crashing at the time of run?
if yes, what is the core file says?

BR/ KMat
@KMat My program crashes after Name prompt(when trying to ask for password)....I am using windows 7 and debug and close program windows opened....Any idea what cause this?
1
2
           cout<<"Enter Name: ";
           cin>>Name;
'Name' is pointing to garbage. The same occurs with 'Password'.
@ne555 Thanks for pointing out but can you tell me how to fix this problem and allocate memory for them?
Topic archived. No new replies allowed.