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;
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.
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 putusingnamespace 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.
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.
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?
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>
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/