Hello everyone, this is my FIRST EVER program written in ANY language. It demonstrates the use of Microsoft Visual Studio 2010 Express C++ to create a program that shows virtual polymorphism. Enjoy my first ever program!
//Demonstration of a "virtual polymorphism" program using Microsoft Visual Studio C++ 2010 Express.
//"Virtual Polymporphism is the ability to process objects differently depending on their data type"
//"or class. More specifically, it is the ability to refine methods of derived classes."
#include <iostream>
usingnamespace std;
class figure {
protected:
double x, y;
public:
void set_dim(double i, double j=0) {
x = i;
y = j;
}
virtualvoid show_area() {
cout << "No area computation defined ";
cout << "for this class.\n";
}
} ;
class triangle : public figure {
public:
void show_area() {
cout << "Triangle with height ";
cout << x << " and base " << y;
cout << " has an area of ";
cout << x * 0.5 * y << ".\n";
}
};
class square : public figure {
public:
void show_area() {
cout << "Square with dimensions ";
cout << x << "x" << y;
cout << " has an area of ";
cout << x * y << ".\n";
}
};
class circle : public figure {
public:
void show_area() {
cout << "Circle with radius ";
cout << x;
cout << " has an area of ";
cout << 3.14 * x * x << ".\n";
}
} ;
int main()
{
figure *p; // create a pointer to base type "figure *p".
triangle t; // create objects of derived types to "triangle t".
square s;
circle c;
p = &t;
p->set_dim(10.0, 5.0);
p->show_area();
p = &s;
p->set_dim(10.0, 5.0);
p->show_area();
p = &c;
p->set_dim(9.0);
p->show_area();
return 0;
}
Is this good for a beginner in ANY language? I'm teaching myself C++ with no help at all. I skimmed through a book i have on it, and managed to write this in about an hour. I know pretty much NOTHING about programming, and managed to teach myself a little bit in about an hour. Do I suck at programming, and do you think i could ever get good at it?
This being your first program is hardly an indicator of whether you're going to be a competent programmer. This just tells me you're able to grasp basic OOP and pointers. This is good, but how good you'll be depends on how much time you're willing to give programming.
It may not be an indicator, but "bloody well done!".
Programming is more about solving problems than it is about learning languages, but you do have to learn languages. The guy who doesn't know any programming languages can hardly be called a programmer just because he's a good problem-solver.
Judging from what's up there, I'd say you're doing something right. You're picking up the tool (C++) quickly, and if what's up there is any indication, I think with perseverance you could become an awesome programmer (problem-solving tool-user).
Actually, don't bother with programming, just bottle whatever it is you're drinking and open a store :-)