c++ convert to oop(object oriented programing)

Pages: 123
Hello, I just want to have couple question about this code,. how to convert this code to oop(object oriented programing). please help me
Last edited on
How to convert it is not a small question but the first very important step where you decide on what the objects are that you want to model as classes, and perhaps not just encapsulate the objects using structs, arrays, even menus (user interfaces).

There is an obvious one to start with and that is the one struct pemilu (Election class)

Next you might want a Candidate class and have an array of them encapsulated (stored) in an Electorate class.

The possibilities are up to you and what sort of problem your system needs to address.

can you help, i'm still confused with this
there is a program to determine the chairperson by choosing
Sounds good. Keep going.
Hello Miskok,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



I ran across this late, but here are some things that should help you.

You should compile your code before posting.

I found that line 9 void ShowResult(char candidates[][50], int *results); ShowResult the piece at the end is a problem.

Whether you forgot to delete this or had something else in mind I do not know.

In the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int Max(int *arr)
{
	int max = 0;

	for (int i = 1; i < N; i++)
	{
		if (arr[i] > arr[max]) {
			max = i;
		}
	}

	return max;

	system("pause");

	return(0);
}

A couple of blank lines helps.

Lines 14 and 16 are never reached because line 12 will leave the function before that.

In line 7 the starting { at the end of the line is confusing when all the others are in a different style. It helps to be consistent in what you use.

againtry makes a good point and to that I would add to that use a "std::string" and "std::vector". These are better choices for an "opp".

#define N 3 works, but you should try to avoid using the "#define" in favor of constexpr size_t MAXSIZE{ 3 }; or const size_t MAXSIZE{ 3 };. Where "size_t" is an alias for "unsigned int". Since most things that you would use it for require a positive number it helps. Also some member functions of the STL classes have functions like "size()" and "length()" both of which return a "size_t" type. And there are other functions that return a "size_t" or "size_type" variable type.

Also try to avoid using single letter variable names in favor of something that is more descriptive.

At the end of the struct you define data[3];. Using "MAXSIZE" is a better choice in case you need to change the size of the array.

In the "switch" data[N].choise). First using "MAXSIZE" may have caught your attention. "data" is defined as having a size of 3. these elements are 0, 1, 2 for a total of 3. What you are using is a memory location that is outside the boundary of the array that it does not own. The results are undefined as you do not know what is at that bit of memory and if you write to that memory you have no idea what you are changing, another variable, program code or something outside the program.

If you are going to use the function std::tolower() you need the header file "<cctype>".

Other than the "swiitch" there are other places where you use data[N]. This will give you a problem.

Using "system" anything is not the best idea. In place of the system("pause"); I use this:
1
2
3
4
5
// A fair C++ replacement for "system("pause")". Or a way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();


I have found it useful.

When writing the new program work in small steps. Compile and test often.

Hope that helps,

Andy
help me, I still can't understand that. can you help me to change the program to opp
can you help me to change the program to opp

Tell us what that means, without using the acronym "OOP".
the above program is still using the procedure, I want to change the above program into an object orientation program
if not, change the struct program above to class
And procedures become methods.
Tell us what the program does... Or what it is supposed to do...
the program is to elect a chairman ... the program is ready but I want to change to OOP
You need classes and a model they operate within.
See: http://www.cplusplus.com/doc/tutorial/classes/

Adapt the example on Rectangles and then come back and show us a Candidate class with a test main()

Candidate just needs two (private) data members called nama_saya_kecil and nama_say_besar of type <string> and a method (procedure) to join the two strings and make nama_saya

You can do it! Just look at the rapid progress you've made so far.
Terimah kasih banyak for being so focussed. Bagus, orang itu.
yes, thank you ... for turning struckt into a class how it is
Tidak bagus :[

You can do sama sama with a struct or a class, there is not a lot of difference.
Read the reference I gave you on classes, there is another one on structs.

Too much makan angin and not enough coding :)
the program is ready but I want to change to OOP


That means "designed with objects". That's all it means. You can write OOP code without classes. You can write OOP style C. OOP is a way of thinking, a way of designing.

So when you say "change to OOP" it doesn't mean anything.

Do you mean "I want to create one class, and all the functions should then be class functions of that one class?"

You can do that if you want; it would be hideous because then you'd be using a class as a bag of unrelated functions which is generally considered to be what happens when OOP is misunderstood and has gone very wrong, but you can do it.
Last edited on
Well it seems to me that the OP doesn't know anything to begin with.

Apart from how to rip out comments and change some string constants, the code looks pretty identical to this 2013 relic.
https://eidelbertsinaga.wordpress.com/2013/11/20/aplikasi-tugas-akhir-c-aplikasi-pemilihan-presiden-indonesia-2014/

Trying to palm off "found code" and asking people to change it for you is not how to learn.

If you've been "phoning it in" all semester, you deserve to fail.
:)
Here's one way to do it. I created 3 classes.

Class Candidate has info about a single candidate
Class Election has a vector of candidates and methods to add a vote and select a winner
Class ElectionUI is a user interface to an election.

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
// Info about a single candidate
struct Candidate {
    string name;
    unsigned votes {0};

    Candidate(const string &n) : name(n) {}

    // Print info about the candidate to the stream. "Num" is the
    // index number of the candidate
    void print(ostream &os, unsigned num) const {
        os << "\t" << num << ". " << name << "\t" << votes<< '\n';
    }
};

// An election
class Election {
    vector<Candidate> candidates;
public:
    void addCandidate(const string &name); // add a candidate
    void addVote(unsigned index);          // add a vote for the candidate
    int winner();                          // return the winner's
                                           // index, or -1 if there is
                                           // no winner

    // Only allow const access to the candidates. We don't want any cheating!
    const vector<Candidate> &getCandidates() { return candidates; }
};

// A user interface for an election
class ElectionUI {
    Election &election;         // The UI refers to an election object
public:
    ElectionUI(Election &el) : election(el) {}

    void showBanner();          // Show the opening batter
    void showMenu();            // Show the menu
    char getChoice();           // get a menu choice
    void showResult();          // Show the election results
    void showWinner();          // show the election winner
    void addVote();             // add a vote for a candidate
};


With these classes, the main program becomes:
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
int
main()
{
    Election election;
    ElectionUI ui(election);

    election.addCandidate("kandidat1");
    election.addCandidate("kandidat2");

    while (true) {

        ui.showMenu();
        switch(ui.getChoice()) {
        case 'q':
            ui.showWinner();
            break;
        case 'r':
            ui.showResult();
            break;
        case 'v':
            ui.addVote();
            break;
        case 'e':
            return 0;

        default:
            cout << "you wrong!\n\n";
        }
        cout << endl;
    }

    system("pause");
    return 0;
}


See if you can create the methods for Election and ElectionUI. You already have most of the logic, you just need to move it around.
Pages: 123