Is this good for my FIRST EVER program?

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!

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
66
67
68
69
70
71
72
  //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>
using namespace std;

class figure {
protected:
  double x, y;
public:
  void set_dim(double i, double j=0) {
    x = i;
    y = j;
  }
  virtual void 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?
Last edited on
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.
ouch! expected hello world(maybe in its advanced form?). +1

Aceix.
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 :-)

p.s.
That's a rectangle, not a square.
Last edited on
Topic archived. No new replies allowed.