[try Beta version]
Not logged in

 
OOP statements without objects!!

Oct 3, 2018 at 7:23am
Shown below is a code culled from a programming text I'm reading; the question the code attempts to answer is also shown.
Oct 3, 2018 at 7:58am
Oops! Sorry guys; here is the complete post.

Shown below is a code culled from a programming text I'm reading; the question the code attempts to answer is also shown.

Question: Write a program that takes a set of events, and determines the maximum number of events that take place concurrently.


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
struct Event {
    int start, finish;
};

struct Endpoint {
    bool operator<(const Endpoint& e) const {   
         //if times are equal, times corresponding to start come first 
	 return time != e.time ? time < e.time : (isStart && !e.isStart);
    }

    int time; 
    bool isStart;
}; 


int FindMaxSimultaneousEvents(const vector<Event>& A) {
    //Builds an array of all endpoints
    vector<Endpoint> E;
    for(const Event& i : A) {
       E.emplace_back(Endpoint{i.start, true});
       E.emplace_back(Endpoint{i.finish, false}); 
    }

    //Sorts the endpoint array according to the time, breaking ties by printing start times before end times
    sort(E.begin(), E.end()); 

    //Track the number of simultaneous events, and record the maximum number of simultaneous events
    int max_num_simultaneous_events = 0, num_simultaneous_events = 0;
    for(const Endpoint& e: E) {
       if(e.isStart) {
           ++num_simultaneous_events;
           max_num_simultaneous_events = max(num_simultaneous_events, max_num_simultaneous_events);
       } else {
         --num_simultaneous_events;
       }
    }
    
    return max_num_simultaneous_events;

}



By the way, I fully understand the workings of the code but I do have a question about a recurring C++ programming feature that has nagged me for a while. It concerns the statements in Lines 20 and 21: I expect the items being emplaced into vector E to be objects; not types. I do not see any declared objects being inserted/emplaced here; rather, I see the type Endpoint being used. Why?

It is objects (of types) that are initialized, not types! What is going on here?
Oct 3, 2018 at 8:22am
Endpoint{i.start, true}
This creates an (unnamed) object of type Endpoint.

E.emplace_back(Endpoint{i.start, true});
is
E.emplace_back(an object of type Endpoint made using the call Endpoint{i.start, true} );

There is an object being created right there.
Oct 3, 2018 at 8:40am
What kind of object is that?

If "there is an object being created right there" as you said, doesn't it violate the format/syntax for C++ object declaration statements shown below:
Type object (initialization values);

It appears the format being used here is:
Type (initialization values);

No object of type Endpoint was earlier declared locally in, or passed into, function FindMaxSimultaneousEvents; how is that acceptable?

Last edited on Oct 3, 2018 at 8:53am
Oct 3, 2018 at 8:52am
What kind of object is that?

An Endpoint object.
More accurately, a temporaryEndpoint object.

If "there is an object being created right there" as you said, doesn't it violate the format/syntax for C++ object declaration statements

You've missed out the different C++ syntax for creating temporary objects.

Last edited on Oct 3, 2018 at 9:52am
Oct 3, 2018 at 9:08am
You are correct that it does violate the syntax for object declaration statements, because it's not a statement (it's an expression), and I don't think you can call it a declaration either because the object does not have a name.
Last edited on Oct 3, 2018 at 9:10am
Oct 3, 2018 at 9:53am
how is that acceptable?


It's acceptable because it's correct C++. If you don't want to accept it, then either don't use it or find a different programming language.
Oct 3, 2018 at 10:36am
@ Repeater:

You should strive to substantiate your posts with instructive statements, clear explanations, and unimpeachable C++ concepts, not disconcerting phrases like "either don't use it or find a different programming language."

If you cannot defend your submissions in a forum like this where intelligent back-and-forth debate/discussion is encouraged, then kindly don't comment.
Oct 3, 2018 at 11:02am
@geeloso

I disagree.
Topic archived. No new replies allowed.