pointer as data members

This is a discussion on pointer as data members within the C++ Programming forums, part of the General Programming Boards category; i have to create a class of student with the following data members, Code: class student { char *name; int ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    pointer as data members

    i have to create a class of student with the following data members,
    Code:
    class student
    {
          char *name;
          int age;
          int *regNum;
          int *address;
          int cgp;
    i dun know how to intialize them in argumentless constructor ,, and set the values in argument constructor and copy constructor.
    can anybody help??

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The only meaningful value you can initialize them with without any arguments to the constructor, is 0 for the pointers. Use an initialization list for that.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    I would rather set pointers to NULL.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Quote Originally Posted by Elysia View Post
    I would rather set pointers to NULL.
    Stroustrup says you should use 0.

    Stroustrup: C++ Style and Technique FAQ

    I use NULL in C and 0 in C++.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    He does not say you should use 0. He says he prefers to avoid macros, hence uses 0.
    I don't agree. NULL is supposed to be a null pointer constant and thus should be used to initialize pointers (but nowhere else).
    NULL shall eventually be replaced with nullptr, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
     student()
       {
         name=new char(' ');
         age=0;
         regno=new int(0);
         address=new int(0);
         cgp=0;
       }
    
      student( char* nm, int ag, int* rn, int *ad, int cg)
       {
         strcpy(name,nm);
         age=age;
         regno=rn;
         address=ad;
         cgp=cg;
       }
    ohk above is the parameterless and with parameter constructor,,
    Can anbody help mee with the copy constructor??

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I am guessing that your motivation for having pointer member variables is that you want to have dynamically allocated arrays. If so, I suggest that you ditch the pointers in favour of:
    Code:
    #include <string>
    #include <vector>
    
    class student
    {
    public:
        // ...
    private:
        std::string name;
        std::vector<int> regNum;
        std::vector<int> address;
        int age;
        int cgp;
    };
    Now, if it is the case that due to artificial assignment requirements you are not allowed to use std::string and std::vector, then you should create two more classes: a string class and an integer array class. You would then substitute them for std::string and std::vector<int>.

    The reason why I suggest this is that it can be rather challenging to manually manage memory correctly for more than one dynamically allocated array simultaneously.

    As for what you have now:
    Code:
    name=new char(' ');
    This looks wrong. It dynamically allocates a single char by itself, but if you wanted to do that, then you might as well just have a single char as a member instead of having a pointer. You should use new[] instead of new (and thus use delete[] instead of delete).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<string.h>
    class student
      {	char *name;
    	int   age;
    	int  *regno;
    	int  *address;
    	int   cgp;
    private:
      student()
       {
        name=new char[10];
        age=0;
        regno=new int(0);
        address=new int(0);
        cgp=0;
       }
    
      student( char* nm, int ag, int* rn, int *ad, int cg)
       {
        strcpy(name,nm);
        age=ag;
        regno=rn;
        address=ad;
        cgp=cg;
       }
      
      ~student()
      {
    	  delete name;
    	  delete regno;
    	  delete address;
      }
    
    
       student(const student &k)
       {
        name=k.name;
        age=k.age;
        regno=k.regno;
        address=k.address;
        cgp=k.cgp;
       }
    
       void setname()
       {
       cin>>name;
       }
    
       char* getname()
       {
       return name;
       }
    
       void print()
       {
       cout<<name<<endl;
       cout<<age<<endl;
       cout<<regno<<endl;
       cout<<cgp<<endl;
       }
      };
    
    int main()
    {
    clrscr();
    student a;
    student b;
    a.setname();
    a.getname();
    a.print();
    b.print();
    getch();
    return 0;
    }

    Student::student() is not accesible
    student::~student() is not accessible
    student::setname(), getname() not accessible

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    They are declared with private access instead of public access.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    You have other problems in the code, as well...
    Code:
      student( char* nm, int ag, int* rn, int *ad, int cg)
       {
        strcpy(name,nm);
        age=ag;
        regno=rn;
        address=ad;
        cgp=cg;
    }
    strcpy is unsafe - you do not know how long the string you try to copy is. It may cause buffer overruns. That is why you should use std::string.
    For rn and ad (extremely poor names, btw, you should make them more descriptive), get have a memory leak. The reason is that you allocate memory, but never free it before replacing the pointer's value with something else.
    Even worse is that you simply copy the pointers over and never change the object you copy from.
    As it stands, when either object goes out of scope, it will delete its data. But then, when the second object goes out of scope, it will try to delete the data that has already been freed, thus causing undefined behavior.
    I suggest you avoid pointers.

    Also, setname is wrong. It should not ask for name; setname is a function that names the object. Hence, it should take its new as a parameter and let the caller be responsible for attaining the name and passing it along.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    oops sorry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21