473,383 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

how to call parameterized constructor in derived class

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
1 999
dev7060
636 Expert 512MB
- 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

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

Similar topics

9
by: PengYu.UT | last post by:
Hi, I have the following class. I want the constructor gnuplot_2d(int size) has the same behavior as if I call from the 2-argument constructor gnuplot_2d(size, size). But the following...
3
by: J.J. Feminella | last post by:
(Please disregard the previous message; I accidentally sent it before it was completed.) I have source code similar to the following. public class Vehicle { protected string dataV; // ......
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
3
by: Elia Karagiannis | last post by:
The static constructor of a derived class never gets called if it has no other methods and the base class is only static I have the following base class and derived class. public class MyBase...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
2
by: Ethan Strauss | last post by:
Hi, I want to be able to make a Master constructor for a class which all overloads of the class constructor would call and thus if I have minor changes to something I only need to make them in the...
11
by: Rahul | last post by:
Hi Everyone, While working with Java, i came across super() which passes values to base class constructor from derived class constructor. I was wondering if this could be implemented in c++ by...
3
by: LamontSharp | last post by:
"Close but no cigar" This code does everything great until the end where for many days I have been trying to maintain the current outputs but somehow add a display where WorTN q and Tnum x print...
1
by: LamontSharp | last post by:
This code compiles nicely - i am looking for print methods to make the output print the format shown below - i am looking for a function or method of calling the existing code objects to make them...
1
by: monalijoshi | last post by:
Hi, As i have one Abstract base class say MyBase. It have parameterized constructor with string value it have abstract method call. And I also have One child class say MyChild. It does not have...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.