#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Smile Problems and questions related to C and C++


    Hello friends ,
    Q1.
    Perhaps my 1st C++ program after a long time but has errors.


    Code:
    #include<iostream>
    #include<conio.h>
    
    using namespace std;
    class rectangle
    {
    	private:
    		int len,br;
    	
    	public:
    		
    		void getdata()
    			{
    			cout<<"Enter length and breadth of rectangle"<<endl;
    			cin>>len>>br;	
    			}	
    	
    		void setdata(int l, int b)
    			{
    				len=l;
    				br=b;
    				
    			}
    			
    			
    		void display()
    			{
    			int area,perimeter;
    			area=len*br;
    			perimeter=2*(len+br);
    			cout<<"Area of rectangle is "<<area<<endl;
    			cout<<"Perimeter of rectangle is "<<perimeter<<endl;
    			}
    }
    
    
    
    
    
    void main()
    {
    rectangle r1,r2,r3;
    cout<<"Rectangle r1 "<<endl;
    r1.setdata(2,5);
    r1.display();
    cout<<"Rectangle r2 "<<endl;
    r2.getdata();
    r2.display();
    cout<<"Rectangle r3 "<<endl;
    r3.setdata(4,4);
    r3.display();
    
    
    
    getch();
    
    }
    Q2. I wonder why main must return something? ie why main can be int do we ever call main in other function?I have never seen main being called in other function and see it as intermediate function..I maybe wrong but I dont know why.


    Q3.
    Mostly the programs I see i dont see a getch() or something.... but whenever I do programs I have to compulsoraily use getch else command screen vanishes .. i wonder why ppl dont use it and still how they view output...
    in 99% cases i didnt see usage of getch...

    Q4.
    I am using Dev C++ for coding......

    Code:
    #include<iostream.h>
    main()
    {
    blah.. blah.. blah;
    }
    here it warns me that you are using depriciated header.... so i
    try this way

    Code:
    #include<iostream>
    using namespace std;
    what is this way? I didnt understand..
    eg: if you want to use printf in c you dont need anything just stdio.h file

    but for c++ i see if you dont use namespace std we have to use like this
    Code:
    std:cout<<"Hello World!!";

    I would be thankful to know their answers... my questions may sound silly but please bear with newbies :D
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > Q1.
    > Perhaps my 1st C++ program after a long time but has errors.
    You should post your error messages.
    But from the look of things, it seems your class is missing a ; at the end.
    Code:
    class foo {
        // stuff
    };  //<< here!

    > Q2. I wonder why main must return something?
    You don't call it, but the run-time library (and by extension, the OS) does call it.
    The return result of main is typically available in your shell in some form or another.
    In Linux (and other unices), this is in some kind of status shell variable.
    Code:
    $ grep something file
    grep: file: No such file or directory
    $ echo $?
    2
    $ grep floom readme.txt 
    $ echo $?
    1
    Here the 'grep' program would have had 'return 2;' in the case that the file could not be found, and 'return 1;' in the case that nothing was found in a file. It returns 0 if something was found.
    If you were writing a script to do more work, these status results could guide decisions within the script.

    > Q3.
    > Mostly the programs I see i dont see a getch() or something....
    Programs are typically run from the command line outside the IDE.

    > but whenever I do programs I have to compulsoraily use getch else command screen vanishes
    Only when you run the program within the IDE.
    Better IDE's have the option to "keep window open at program exit", which removes the need for hacking the code to pause at the end.

    > .. i wonder why ppl dont use it and still how they view output... in 99% cases i didnt see usage of getch...
    Like I said, people don't run code in the IDE when you've finished writing the program.
    Also, getch() is only specific to archaic DOS (and things which retain some memory of that forgotten age of computing).

    > Q4.
    > I am using Dev C++ for coding......
    If you got that from bloodshed.net, then it's obsolete. Win2K was the last supported OS IIRC.
    Try something more up to date.
    http://www.microsoft.com/express/Downloads/
    http://www.smorgasbordet.com/pellesc/
    http://www.codeblocks.org/
    http://sourceforge.net/projects/orwelldevcpp/
    In fact, it's a useful test of your code to make it compile with several compilers and get the same result. Making something work with one compiler retains an element of luck (the code still works despite the bugs). But compiling it with two compilers flushes out a lot of otherwise hidden bugs.

    > here it warns me that you are using depriciated header.... so i try this way
    Yes, standard C++ header files ending in .h were made obsolete in 1998!
    The C++ standard of that year introduced the concept of namespaces.

    > what is this way? I didnt understand..
    > eg: if you want to use printf in c you dont need anything just stdio.h file
    Imagine a large project, using many libraries from many different sources.
    Now imagine that two separate libraries manage to define a function called 'readFromFile'. How do you distinguish between the two?

    In C, this can be a real PITA to resolve.

    In C++, it's easy - providing the respective library authors have made good use of namespaces. Calling each function becomes a simple matter of writing
    foo::readFromFile();
    or
    bar::readFromFile("file.txt");

    Writing using namespace std; is rather a lazy way out - it should be regarded as an easy way of getting pre-standard C++ to compile with standard compliant compilers.
    Ideally, you should write
    std::cout << "Hello world" << std::endl;

    You can gain some convenience by writing
    using std::cout;
    then
    cout << "Hello world" << std::endl;

    In other words, use 'use' sparingly, for only what you're most interested in.
    It will make your life a lot easier when you start to work on projects with multiple namespaces.

    Comments on this post

    • swapy agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo