By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,775 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,775 IT Pros & Developers. It's quick & easy.

how to call parameterized constructor in derived class

P: 1
class Time { public: int hh, mIn, ss; Time() { hh = mIn = SS = 0; } } ;
(a) [15%] Implement a class named "Event" that satisfies all of following requirements: i) The class has the following data members - id: An integer - name: A string that represents the name of the event - start, end: The starting and ending times ofthe event. We assume all events starts and ends in the same day.
ii) The class has two constructors a) A default constructorJa constructor with no parameters) that sets the data members to the following values: Data members Initial values Id 0
Name
"No name"
start, end
Both represent the time 00:00:00.

Here from the above part how to use start and end time with the base class construtor

Mine code is
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std ;
  4.  
  5.  class Time {
  6.  public:
  7.  int hh, mm, ss;
  8.  
  9.  Time(){
  10.  hh =mm =ss =0;
  11.  }
  12. // friend class Event;
  13. Time :: Time(int h, int m, int s)
  14. {
  15.     hh=h;
  16.     mm=m;
  17.     ss=s;
  18. }
  19.  };
  20.  
  21.  
  22. class Event : public Time
  23. {
  24.  
  25. public:
  26.     int id ;
  27.     char name[10] ;
  28.     float start ;
  29.     float End;
  30.  
  31.  
  32.     Event(){
  33.         id = 0;
  34.         strcpy(name, "Hi");
  35.         start = 0.0 ;
  36.         End = 0.0 ;
  37.     }
  38.  
  39. //    Event();
  40.     Event(int id1, char name1[],float start1, float end1){
  41.         id = id1 ;
  42.         strcpy(name,name1);
  43.         start = start1 ;
  44.         End = end1 ;
  45.         cout<<id<<endl<<start<<endl<<name<<endl<<End<<endl ;
  46.         //cout<<"Done"<<endl ;
  47.     }
  48.  
  49.  
  50. };
  51.  
  52. int main(){
  53.  
  54.     Event e ;
  55.     Event(300, "hi",3.0 , 9.0) ;
  56.         return 0 ;
  57. }
Jul 10 '19 #1
Share this Question
Share on Google+
1 Reply


dev7060
100+
P: 115
- Scope resolution operator is not required before parameterized constructor of class Time.

- Why is there a parameterized constructor for the Time class if it's not even being used?

- Provided text says, start & end: The starting and ending times of the event. Both represent the time in the format hh:mm:ss.

- It is quiet clear that any primitive data type can't hold a value in such format. However, it can kinda be stored in a character array but it'll then create issues in further calculations as the data type is not suitable for storing such values.

-User defined data type structure can be used in such case. The structures can be declared inside Time class like:
Expand|Select|Wrap|Line Numbers
  1.  struct s {
  2.      int hh;
  3.      int mm;
  4.      int s;
  5.  };
  6.  struct e {
  7.      int hh;
  8.      int mm;
  9.      int ss;
  10.  };
  11.  struct s start;
  12.  struct e end;
  13.  
-All the values can be initialized in constructor as:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.  start.hh=h1;
  3.  start.mm=m1;
  4.  start.ss=s1;
  5.  end.hh=h2;
  6.  end.mm=m2;
  7.  end.ss=s2;
  8. ...
  9.  
where h1, m1, h2 etc. are formal arguments of constructor.

- These values can be initialized in default constructor as
Expand|Select|Wrap|Line Numbers
  1.  start.hh=start.mm=start.ss=end.hh=end.mm=end.ss=0;
  2.  
Jul 10 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.