Thread: Conversion Constructor and Pointers Help

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Conversion Constructor and Pointers Help

    Hello group!!This is my first thread here,so bear with me.. I have been assigned a program to study about pointers and constructors all at once and since i am a beginner,i am not able to figure out how this program works.
    :
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<alloc.h>
    class A
    {
    	private:
    	int a;
    	public:
    
    A()
    {
    	a=30;
    }
    void show()
    {
    	cout<<"\n a="<<a;
    
    }
    A(int x)
    {
    this->a=x;
    }
    };
    
    int main()
    {
    clrscr();
    
    A k,*b,a;
    *b=50;
    k=*b;
    
    b->show();
    a.show();
    k.show();
    cin.get();
    return 0;
    }
    :
    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    So compile it and run it. What does it show? Or will it crash even? What happends? What exactly are you having trouble with?

    Pointers in C and C++ - Tutorial - Cprogramming.com
    Last edited by Shakti; 09-15-2011 at 11:16 AM.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    No trouble with the output dude...it is

    a=50
    a=30
    a=50


    my question is,how does the line

    "this->a=x" work?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    "this" is a pointer that is present in all classes and basicly points to the instance of the object you are calling the method from and can only be used within memberfunctions of a class. That line is equivalent to writing a=x, no difference but some people like to type out this->a to make it more apparent that you are working on a member variable or calling a member function.

    More info on this pointer: 8.7 — The hidden “this” pointer « Learn C++'

    Edit: Also it is pure luck that your code actually works and does not crash. You have a pointer that isnt initialized to any memory at, yet used and assigned to. This is very dangerous and has caused alot of problems for alot of programs where they "suddenly stop working".

    And last but not least, you are using non-standard c++ headers and any modern compiler should warn you or give you errors for those. iostream.h should be iostream, don't need alloc.h and the only reason you have conio.h is for clrscr which is also nonstandard. I would suggest you upgrade to a compiler that warns or gives you errors for these things. 2 Suggestions i have are http://www.codeblocks.org/ and http://www.microsoft.com/visualstudi...itions/express
    Last edited by Shakti; 09-15-2011 at 12:44 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes I also noticed immediately that the code both assigns to and reads from an uninitialised pointer.

    My advice was going to be to step through the program one line at a time using the debugger and look around at what is called and what is changed as it runs. However, if you try and run that program then it will most likely just crash.
    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
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thanks a lot pal:-)that was helpful:-)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pointers do not actually allocate objects. Just as their names imply, they "point" to things. So to actually use a pointer, you must first point it to some valid memory location.
    This can be done in several ways. But you can, for example, do b = &a to take the address of a and have b point to it. At that point forward, you can manipulate b as you wish.
    Keep this in mind.
    Also, better indentation wouldn't hurt.
    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.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thanks Elysia:-)Now i get a clearer picture of the whole:-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Constructor help
    By AvtrOfWoe in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2010, 02:05 AM
  2. expected constructor, destructor, or type conversion before '('
    By cosmiccomputing in forum C Programming
    Replies: 5
    Last Post: 06-16-2008, 11:03 PM
  3. Conversion constructor for template class
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-08-2008, 01:22 PM
  4. pointer conversion problems with a copy constructor
    By stanlvw in forum C++ Programming
    Replies: 8
    Last Post: 01-14-2008, 12:06 AM
  5. Conversion and Constructor Functions
    By shiv_tech_quest in forum C++ Programming
    Replies: 5
    Last Post: 03-06-2003, 06:39 AM