HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

"Hello, world!" program using a class for printing

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
programworldprintingusingforhelloclass

Problem

Please take a look at my program and let me know how I can improve it.

/*
"   To Print A Line On The Display Screen"
    Date:5th January 2011
    Programmer:Fahad
*/
#include 
using namespace std;
class Print
{
    public:
        void print_();
};
int main()
{
    Print Obj;
    Obj.print_();
    system( "pause" );
    return 0;
}
void Print::print_()
{
    cout << "I am in print function and the program runs fine." << endl;
}

Solution

In addition to the other comments, I would also use a different naming convention for types and objects.

For example, this looks unconventional.

Print Obj;
Obj.print_();


I prefer:

Print obj;
obj.print();


It's just a convention but being able to easily spot names that denote types helps if you start to use more complex expressions. For example:

Print().print();


Personally, I would also avoid system("pause"). You need to #include either ` or ` to use it. Although the system call itself is standard C++ (from the standard C library), what you pass to it is system dependent.

In general I don't believe you should make your programs stop artificially. If they are designed to run in a terminal then the terminal user will be able to see the output even after the program exits.

Code Snippets

Print Obj;
Obj.print_();
Print obj;
obj.print();
Print().print();

Context

StackExchange Code Review Q#604, answer score: 18

Revisions (0)

No revisions yet.