Namespace - regarding.

Hello there,

I am just learning Cpp. I am learning inheritance , I did a simple program.

It was basically creating a base class and a derived class. I used to normally write a normal base and derived class. But then, when I went through a site, I came through a very new concept. Namespace.

This is my program down there. I never used to type std::cout nor use using namespace std; ever before. I just initialize the headerfle(iostream) and then use cout or cin directly and I have got the output. Why is this new namespace concept?
And the below program, I am not able to get the output. I am getting errors.
I use turbo c++.
I am a beginner, please dont mind, i am naive. :)

2) It would be great if you could explain what return does. I sort of know return 0, it is basically returns 0 once the program is over. But what is return(height*width). what is that return?



#include <iostream>

using namespace std;

// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};

int main(void)
{
Rectangle Rect;

Rect.setWidth(5);
Rect.setHeight(7);

// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;

return 0;
}
return(width * height);
will return your area(multiplication of 5 * 7).

return 0 will terminate you main function and memory will be freed.
Could you put your code in code tags using the <> button on the right, much easier to read.

So what errors are you getting - can you post the compiler output?

return sends info back to where the function was called. In other words it is giving the answer to the question. The question is the function call getArea(). the function calculates the area, it is returned to the place the function was called (the cout statment). Cout prints it in a readable form.


Edit: Is this an error?

class Rectangle: public Shape

Should it be :

class Rectangle public: Shape
Last edited on
Ok, my question is mainly about namespace.

What I understood from certain threads of this forums is that, when ever we use cout or cin in c++ we are supposed to use either unsigned namespace std; or std::cin std::cout. why is that? I never used any of these in my program where I used inheritance. Can you explain me about this 'namespace'.
Namespaces are supposed to keep names of variables separate from each other. So if I made a large app I would create a namespace for it so that my variable names wouldn't have the same names as someone else's app.

There is a global namespace, and a std namespace which has a lot of the STL stuff, like cin, cout, strings, vectors etc in it. The trouble is that if you put using namespace std; at the start of your file, it pollutes the global namespace with heaps & heaps of stuff, so it is better to std:: before each standard item like cin, cout, endl, strin, vector etc.

This has got nothing to do with inheritance, and I am surprised you haven't come across it before, any time you want to do cout, you need to do either:

std::cout

or put this at the top of your file:

using std::cout;

Google "C++ namespace example" to read all about it.
Why is this new namespace concept?

It is actually very old, it was introduced in 1990, while C++ was just barely taking shape. Namespaces are an integral part of the language: they

1) group names together (so that, you can link two different libraries that both declare a function "max()", for example)

2) form public interfaces of classes (so that you don't have to use member functions to provide a public interface)

3) provide a way to hide names that don't need to be exposed (either as "detail" namespace, common in many libraries, or as the unnamed namespace)

I use turbo c++

Even in 1992, when your compiler was released, it was rather outdated and didn't implement namespaces. Much has changed in 20 years.. C++ was finally standardized in 1998, and then expanded and evolved continuously. Turbo C++ can't compile most C++ programs, as you've discovered. Unless you're interested in early history of this language, get a compiler released in the last year or two.
Last edited on
I understood. But I use class and objects, I never use unsigned namespace std; or std::cout. I got the output .

I will put up the code below and I got the output in turbo c++

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
#include<iostream.h>
#include<conio.h>
 
class emp
{
   public:
     int eno;
     char name[20],des[20];
     void get()
     {
              cout<<"Enter the employee number:";
              cin>>eno;
              cout<<"Enter the employee name:";
              cin>>name;
              cout<<"Enter the designation:";
              cin>>des;
     }
};
 
class salary:public emp
{
     float bp,hra,da,pf,np;
   public:
     void get1()
     {             
              cout<<"Enter the basic pay:";
              cin>>bp;
              cout<<"Enter the Humen Resource Allowance:";
              cin>>hra;
              cout<<"Enter the Dearness Allowance :";
              cin>>da;
              cout<<"Enter the Profitablity Fund:";
              cin>>pf;
     }
     void calculate()
     {
              np=bp+hra+da-pf;
     }
     void display()
     {
              cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";
     }
};
 
void main()
{
    int i,n;
    char ch;
    salary s[10];
    clrscr();
    cout<<"Enter the number of employee:";
    cin>>n;
    for(i=0;i<n;i++)
    {
              s[i].get();
              s[i].get1();
              s[i].calculate();
    }
    cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
    for(i=0;i<n;i++)
    {
              s[i].display();
    }
    getch();
}
i mean, in the above program I just used iostream and i havnt used namespace. even in collage it worked. I sortoff understood what namespace is. Explain why I am not using in the above program. is it the compiler?
@theideasman: what do you mean by this?
1
2
3
4
5
6
7
Edit: Is this an error?

class Rectangle: public Shape

Should it be :

class Rectangle public: Shape
yesh11 wrote:
i mean, in the above program I just used iostream and i havnt used namespace. even in collage it worked. I sortoff understood what namespace is. Explain why I am not using in the above program. is it the compiler?


Yes - you are using a well out of date compiler.
This is shown by the fact that you have this line
#include<iostream.h>

@chipp

My bad - I shouldn't stay up till 4am and write replies without having the brain engaged first.
I use turbo c++

Don't.
I will put up the code below and I got the output in turbo c++

DON'T!
#include<iostream.h>

NOOOOOOOOOOOOOOOOOO!!!!!!!!!!!!!!!!!!!!!!
uninstall your compiler and IDE RIGHT AWAY!!!! Do you reallise you are probably using a compiler that isn't even from this millenium?!?!?!?!?!?!? Your compiler has to be erased without a trace!!!!!!!!!!!!!!!!!!!!! Once your computer doesn't even have a trace turbo-c++ ever existed on it, install this: http://orwelldevcpp.blogspot.com/
Topic archived. No new replies allowed.