Like Tree5Likes
  • 3 Post By antred
  • 2 Post By Rodaxoleaux

constructor and its uses

This is a discussion on constructor and its uses within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, If we have a class as: Code: class student { int roll_no; string name; char student_id; public: student(string=null,int=0,char=null); ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    constructor and its uses

    Hi Guys,

    If we have a class as:

    Code:
    class student
    {
    
     int roll_no;
     string name;
     char student_id;
    
    public:
    
    student(string=null,int=0,char=null);
    
    
    };
    I wanted to know that can we initialize string using constructor?
    Can we initialize string with a "null"?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You can initialize the string in many ways, as an example (using an int instead of a string) consider:
    Code:
    class bar
    {
        int value;
    public:
        bar(int val = 42) : value(val)
        {
        }
    };
    Now I can initialize an object of this particular class in a couple different ways:
    Code:
    bar foo1;      // Creates an object foo1 with value initialized to 42
    bar foo2(5);   // Creates an object foo2 with value initialized to 5
    bar foo3 = 7;  // Creates an object foo3 with value initialized to 7
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    No. A string has a default value of "" (nothing), which "is" NULL for a string. NULL itself is meant for pointer memory values however, in which it is 0x0 (nothing)

    A class constructor can be used to initialise your member variables as you said, by using the name of the class itself as a function with no type or return value.

    Code:
    public:
       student() { name = "Eric"; roll_no = 25; student_id = "99320002"; }
       ~student(){}
    A deconstructor is is called if the class goes out of scope (or is delete'd as a pointer (a constructor is called when new is used or a non-pointer init is called)). What you're trying to do is called an initialisation list. And that's not how it's done.

    Code:
    student() : name(""), roll_no(25), student_id("99320002") {}
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    227
    Quote Originally Posted by Rodaxoleaux View Post
    Code:
    student() : name(""), roll_no(25), student_id("99320002") {}
    Personally, I'd get rid of the red part, seeing as the default constructor of std::string creates an empty string anyway.

    EDIT: Or better yet, give student a c'tor that takes arguments and initializes its members to proper values to begin with.
    iMalc, jimblumberg and Elysia like this.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    Quote Originally Posted by Rodaxoleaux View Post
    A deconstructor is is called if the class goes out of scope
    A destructor is is called if the class goes out of scope.
    FTFY!

    Note that having an empty non-virtual destructor is not desireable. If you destructor does not need to be marked virtual and has no work to do, then it should be deleted.
    Last edited by iMalc; 06-26-2012 at 01:55 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    Thanks for the corrections.
    Yeah, I didn't sleep last night give me a break o.e
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Hi

    Thanks for all your reply,

    so in the class of student:

    --------------------------------------------------------------------------------------------------
    Code:
    class student 
    
    {  
    
    int roll_no;  
    string name;  
    char student_id;   
    
    public:   
    
    student(string,int,char);     //constructor with parameters/
    
    };
    ----------------------------------------------------------------------

    Can we initialize the data members of a class just by using their data types?

  8. #8
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    Quote Originally Posted by student111 View Post
    Can we initialize the data members of a class just by using their data types?
    You said thanks for the replies as if you read them, but your new code suggests that you didn't look or take into account a word that was typed.
    manasij7479 and antred like this.
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    Quote Originally Posted by student111 View Post
    Can we initialize the data members of a class just by using their data types?
    No.

    However, the string in your class is initialised by itself without you doing anything.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Quote Originally Posted by student111 View Post
    Can we initialize the data members of a class just by using their data types?
    What do you mean by using their data type? The example you provided doesn't have much to do with initialization. The line that you bolded is the constructor portion of a class declaration. The most that has to do with initialization is the fact that the compiler will check the types of what you pass in with the types of the parameters to see if they match.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    The updated code seem to reflect the inability to consume the material that has been presented so far.
    I still see a single character instead of a string.
    I still see the inability to add names to the parameters.
    I still see the inability to understand how to initialize members.
    And there is probably more...

    Re-read all those posts you've been given. Consume their contents. Correct your code and ask new questions. If there is something specific about what you've been told so far that you fail to understand completely, then ask specific questions about that.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-23-2012, 10:47 PM
  2. Replies: 5
    Last Post: 02-21-2011, 01:19 AM
  3. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  4. Replies: 6
    Last Post: 05-19-2010, 04:03 AM
  5. Replies: 10
    Last Post: 06-02-2008, 08:09 AM

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