My question is very beginner, and yes I have looked it up extensively, but when I do the things I've found online Xcode gives me errors.
Basically, I'm just curious how to implement a constructor for a derived class. My class is called "Sensor" and the derived classes are digitalSensor and analogSensor.
Here's my sensor.h:
#ifndef __Program_6__sensor__
#define __Program_6__sensor__
#include <iostream>
class sensor {
char* SensorName;
float energyDraw;
int functioning;
int onoff;
public:
sensor(char*n, float pc);
virtual void print();
void setOK(int K);
int getOK();
void setOnOff(int n);
int getOnOff();
};
//---------
class digitalSensor : public sensor {
int reading;
public:
digitalSensor(char*n, float pc);
virtual void print();
void setCurrentReading(int r);
int getCurrentReading();
};
class analogSensor : public sensor {
int Reading;
int minRead;
int maxRead;
public:
analogSensor(char *n, float pc, int mm, int mx);
virtual void print();
void setCurrentReading(int r);
int getCurrentReading();
};
#endif /* defined(__Program_6__sensor__) */
And here's my sensor.cpp, you can see the beginnings of my digitalSensor work at the bottom.
#include "sensor.h"
#include "definitions.h"
using namespace std;
//--------SENSOR CLASS------------//
sensor::sensor(char *n, float pc) {
SensorName = (char*)malloc(strlen(n)+1);
energyDraw = pc;
functioning = WORKING;
onoff = OFF;
}
void sensor::print() {
cout << " Sensor: " << SensorName;
cout << " Power Consumption: " << energyDraw;
if (functioning == WORKING) {
cout << "\nSensor is functioning correctly\n";
if (onoff == ON) {
cout << "Sensor is On";
}
if (onoff == OFF) {
cout << "Sensor is Off";
}
}
if (functioning == NOTWORKING) {
cout << "Sensor is not functioning correctly";
}
}
void sensor::setOK(int k) {
functioning = k;
}
int sensor::getOK() {
return functioning;
}
void sensor::setOnOff(int n) {
onoff = n;
}
int sensor::getOnOff() {
return onoff;
}
//---------------------------------//
//*********DIGITAL SENSOR**********//
sensor digitalSensor::digitalSensor(char *n, float pc) {
}
In a nutshell: I need to make a constructor function for the digital sensor class. What am I missing, how do I do that? Thanks in advance for any help or knowledge on this!
Implement its constructor like this:
digitalSensor::digitalSensor(char*n, float pc) : sensor(n, pc)
{
}
As you can see, it doesn't return anything and it calls its parent's constructor.
But you're doing this which is wrong:
sensor digitalSensor::digitalSensor(char *n, float pc) {
^^^^^^ constructors shall not return anything
}
You should call your base class constructor at the derived class's member initialization list.
For example:
class digitalSensor : public sensor {
int reading;
public:
digitalSensor(char*n, float pc): sensor(n, pc), reading(0){}
virtual void print();
void setCurrentReading(int r);
int getCurrentReading();
};
This defines the digitalSensor constructor inline. You can also define it with scope resolution operator outside class:
digitalSensor::digitalSensor(char*n, float pc):sensor(n, pc), reading(0){}
//^^Note that constructor of a class does not have any return type
It really depends on how your base class constructors are provided. But this could be one way to do it. You may find Initializing base classes and members useful.
Related
This question already has answers here:
Class has no member "Class"
(4 answers)
Closed 2 years ago.
I'm receiving C2600 error (in title) when compiling a simple program making use of custom copy constructors.
My .h file is
#pragma once
namespace uiuc {
class Cube {
public:
Cube();
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
}
My Cube.cpp file is
#include "Cube.h"
#include <iostream>
namespace uiuc {
Cube::Cube() {
length_ = 1;
std::cout << "Default consturctor invoked" << std::endl;
}
Cube::Cube(const Cube& obj) {
length_ = obj.length_;
std::cout << "Copy consturctor invoked" << std::endl;
}
double Cube::getVolume() {
return length_ * length_* length_;
}
double Cube::getSurfaceArea() {
return 6 * length_* length_;
}
void Cube::setLength(double length) {
length_ = length;
}
}
And my main.cpp file is
#include <iostream>
#include "Cube.h"
int main() {
uiuc::Cube c;
uiuc::Cube mycube = c;
return 0;
}
I don't understand why this error is being thrown. Any suggestions?
Edit:
Thanks so much for the quick responses. I'm brand-new to C++, so sorry if this seemed trivial.
The fix, as mentioned by others was to add Cube(const Cube& obj); to the header file.
You didn't declare the constructor in your .h file.
It should look like this:
#pragma once
namespace uiuc {
class Cube {
public:
Cube();
Cube(const Cube& obj); // This was missing.
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
}
Then after that should you be able to define it outside the class definition.
The function:
Cube::Cube(const Cube& obj) {
length_ = obj.length_;
std::cout << "Copy consturctor invoked" << std::endl;
}
is declared in the cpp file as a member function of Cube, but it is missing in your class definition.
It should be in here:
class Cube {
public:
Cube(const Cube& obj)
// ...
};
The following code has three classes. account, savings(derived), current(derived). But three of the memberfunctions from the base class are not getting inherited. This is the error what I get. Is it possible doing without using virtual functions
In file included from main.cpp:1:0:
classes.hpp:96:30: error: no ‘void savingsAccount::deposit()’ member function declared in class ‘savingsAccount’
void savingsAccount::deposit()
^
classes.hpp:105:31: error: no ‘void savingsAccount::withdraw()’ member function declared in class ‘savingsAccount’
void savingsAccount::withdraw()
^
classes.hpp:130:31: error: no ‘void savingsAccount::display()’ member function declared in class ‘savingsAccount’
void savingsAccount:: display()
^
classes.hpp:181:30: error: no ‘void currentAccount::deposit()’ member function declared in class ‘currentAccount’
void currentAccount::deposit()
^
classes.hpp:190:31: error: no ‘void currentAccount::withdraw()’ member function declared in class ‘currentAccount’
void currentAccount::withdraw()
^
classes.hpp:220:31: error: no ‘void currentAccount::display()’ member function declared in class ‘currentAccount’
void currentAccount:: display()
#include<iostream>
#include<cstring>
#define MAX 50
#ifndef classes_h
#define classes_h
using namespace std;
class account
{
protected:
char name[MAX];
int accountNumber;
char type[MAX];
float balance;
float minBalance;
static int Customers;
public:
account();
account(char*,int);
void deposit();
void withdraw();
void display();
int getAccountNumber();
static void numberOfCustomers();
};
account::account(char* name, int accountNumber)
{
strcpy(this->name,name);
this->accountNumber=accountNumber;
//strcpy(this->type,type);
this->balance=0;
Customers++;
}
int account::Customers=0;
void account ::numberOfCustomers()
{
cout<<"Total number of customer-------->"<<Customers;
}
void account::display()
{
}
/********************************
//Savings Account class
********************************/
class savingsAccount: public account
{
protected:
float minBalance;
// float rate;
public:
savingsAccount();
savingsAccount(char*,int);
savingsAccount(account&); //Copy Constructor
/* void deposit(); //user
void withdraw(); //user
void display(); //user
*/ int getAccountNumber();
};
savingsAccount::savingsAccount(char* name, int accountNumber):account(name,accountNumber)
{
minBalance=0;
strcpy(type,"savings");
}
savingsAccount::savingsAccount(account& tp):account(tp)
{
minBalance=0;
strcpy(type,"savings");
}
int savingsAccount::getAccountNumber()
{
return accountNumber;
}
void savingsAccount::deposit()
{
float amount;
cout<<"Enter the amount needs to be deposited"<<endl;
cin >> amount;
balance=balance+amount;
}
void savingsAccount::withdraw()
{
float amount ;
if(balance ==0)
{
cout<<"Account balance is Nil"<<endl;
return;
}
cout<<"Enter the amount yout would like to withdraw"<<endl;
while(1)
{
cin>>amount;
if(balance-amount<0)
{
cout<<"insufficient funds, try some less amount\n";
}
else
{
balance=balance-amount;
return ;
}
}
}
void savingsAccount:: display()
{
cout<<"Account Number"<<accountNumber<<endl;
cout<<"Name-->"<<name<<endl;
cout<<"Accounttype-->Savings"<<endl;
cout<<"Balance-->"<<balance<<endl;
cout<<"Minimum Balance -->"<<minBalance;
}
/***********************************
//Current Account class
************************************/
class currentAccount: public account
{
protected:
float minBalance;
public:
currentAccount();
currentAccount(char*,int);
currentAccount(account&);
/* void deposit();
void withdraw();
void display();
*/ int getAccountNumber();
};
/*
currentAccount::currentAccount(char* name, int accountNumber):account((char*)name,accountNumber,"current account")
{
minBalance=1000;
balance=1000;
}
*/
currentAccount::currentAccount(char* name, int accountNumber):account(name,accountNumber)
{
minBalance=0;
strcpy(type,"Current");
}
currentAccount::currentAccount(account& tp):account(tp)
{
minBalance=0;
strcpy(type,"Current");
}
void currentAccount::deposit()
{
float amount;
cout<<"Enter the amount needs to be deposited"<<endl;
cin >> amount;
balance=balance+amount;
}
void currentAccount::withdraw()
{
float amount ;
if(balance ==0)
{
cout<<"Account balance is Nil"<<endl;
return;
}
cout<<"Enter the amount yout would like to withdraw"<<endl;
while(1)
{
cin>>amount;
if(balance-amount<0)
{
cout<<"insufficient funds, try some less amount\n";
}
else
{
balance=balance-amount;
return ;
}
}
if(balance-amount<1000)
{
cout<<"Please keep the balance above minimum balance ";
}
}
void currentAccount:: display()
{
cout<<"Account Number"<<accountNumber<<endl;
cout<<"Name-->"<<name<<endl;
cout<<"Accounttype-->Current Account"<<endl;
cout<<"Balance-->"<<balance<<endl;
cout<<"Minimum Balance -->"<<minBalance;
}
int currentAccount::getAccountNumber()
{
return accountNumber;
}
#endif
When you do
void savingsAccount:: display(){/* rest here */}
you are telling the compiler to implement the definition of a member function called savingsAccount::display(). However you don't specify that you have this member function in your derived class declarations. Since you overloaded it, you need to add its signature to the derived classes, like
class savingsAccount: public account
{
/* virtual */ void display() /* override */; // probably you want this virtual in the base class
// the rest
};
otherwise the compiler only knows that there exists a base version, i.e. account::display(). So the compiler is indeed right telling you that there is no savingsAccount::display() member function, as you only have account::display(), i.e. the member function of the base class.
Your whole issue can be simplified to
struct X
{
void f();
};
struct Y: X
{
// void f();
};
// error if you don't add the signature in Y,
// there is no Y::f(), only the X::f() inherited part
void Y::f(){}
int main(){}
Live on Coliru
PS: You probably want to make those functions virtual too (and also add const to the ones that don't modify your member variables), so you have proper overriding, otherwise your derived member won't behave as you want when you have a class hierarchy controlled by a pointer-to-base.
PSS: put
#ifndef classes_h
#define classes_h
at the very beginning of your header, before any #include.
I have 4 files car.cpp, car.h, motorvehicle.h and vehicle.h. I am programming in the QT-creator environment.
The issue i am having is:
error: out-of-line definition of 'getSafetyRating' does not match any declaration in 'vehicle::Car'
int Car::getSafetyRating()
Here are the program files for reference, i am new to learning c++ and would really appreciate the help! Cheers Alex. I Apologies in advance for any repost of problems if any.
vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include <string>
namespace vehicle
{
class Vehicle
{
public:
Vehicle(int numberOfPassengers,
int topSpeed,
int numberOfWheels,
std::string color = "red");
virtual ~Vehicle();
virtual std::string getColor();
virtual int getTopSpeed();
virtual int getNumberOfWheels();
virtual int getNumberOfPassengers();
virtual int getSafetyRating() = 0;
protected:
int m_numberOfPassengers;
int m_topSpeed;
int m_numberOfWheels;
std::string m_color;
};
}
#endif // VEHICLE_H
motorvehicle.h
#ifndef MOTORVEHICLE_H
#define MOTORVEHICLE_H
#include "vehicle.h"
namespace vehicle
{
class MotorVehicle : public Vehicle
{
public:
MotorVehicle(int numberOfPassengers,
int topSpeed,
int numberOfWheels,
double kilometresPerLitre);
MotorVehicle(int numberOfPassengers,
int topSpeed,
int numberOfWheels,
std::string color,
double kilometresPerLitre);
virtual ~MotorVehicle();
virtual double getKilometresPerLitre();
protected:
double m_kmpl;
};
}
#endif // MOTORVEHICLE_H
car.h
#ifndef CAR_H
#define CAR_H
#include "motorvehicle.h"
namespace vehicle
{
class Car : public MotorVehicle
{
public:
Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
int numberOfAirBags = 2,
bool abs = true,
int numberOfWheels = 4);
Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
std::string color,
int numberOfAirBags = 2,
bool abs = true,
int numberOfWheels = 4);
virtual ~Car();
virtual int getNumberOfAirBags();
virtual bool hasAutomaticBreakingSystem();
protected:
int m_numberOfAirBags;
int m_abs;
};
}
#endif // CAR_H
car.cpp
#include "car.h"
using namespace vehicle;
Car::Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
int numberOfAirBags,
bool abs,
int numberOfWheels) :
MotorVehicle(numberOfPassengers, topSpeed, numberOfWheels, kilometresPerLitre),
m_numberOfAirBags(numberOfAirBags),
m_abs(abs)
{
}
Car::Car(int numberOfPassengers,
int topSpeed,
double kilometresPerLitre,
std::string color,
int numberOfAirBags,
bool abs,
int numberOfWheels):
MotorVehicle(numberOfPassengers, topSpeed, numberOfWheels,color, kilometresPerLitre),
m_numberOfAirBags(numberOfAirBags),
m_abs(abs)
{
}
Car::~Car()
{
}
int Car::getNumberOfAirBags()
{
return m_numberOfAirBags;
}
bool Car::hasAutomaticBreakingSystem()
{
return m_abs;
}
int Car::getSafetyRating()
{
int SafetyRating = 0;
if (m_numberOfAirBags >= 4)
{
SafetyRating += 3;
}
else if (m_numberOfAirBags >= 2)
{
SafetyRating += 2;
}
else if (m_numberOfAirBags > 0)
{
SafetyRating += 1;
}
if (m_abs)
{
SafetyRating += 2;
}
return SafetyRating;
}
Your car class does not have a getSafetyRating declared in the .h file, and the pure virtual function in vehicle requires it. When you declare something pure virtual, ie func() = 0, you basically say that any class that inherits from it MUST implement this function.
So both the motor vehicle class, and the car class must at the very least declare the getSafetyRating function.
One thing that should solve this is to add this to Motor Vehicle:
virtual int getSafetyRating() = 0;
and in the car.h write
virtual int getSafetyRating()
Add declaration of function int getSafetyRating() in your car.h
class C2; //Forward Declaration
class C1
{
int status;
public:
void set_status(int state);
void get_status(C2 y);
};
class C2
{
int status;
public:
void set_status(int state);
friend void C1::get_status(C2 y);
};
//Function Definitions
void C1::set_status(int state)
{
status = state;
}
void C2::set_status(int state)
{
status = state;
}
void C1::get_status(C2 y) //Member function of C1
{
if (y.status | status)
cout<<" PRINT " <<endl;
}
y.status in the second last line displays an error:
C2::status is inaccessible
The code executes properly, but there is a red line (error) under y.status.
Why is this?
It sounds to me like the compiler (or part of a compiler) used by your IDE has a problem. I added enough to your code to get a complete program:
#include <iostream>
using namespace std;
class C2; //Forward Declaration
class C1
{
int status;
public:
void set_status(int state);
void get_status(C2 y);
};
class C2
{
int status;
public:
void set_status(int state);
friend void C1::get_status(C2);
};
//Function Definitions
void C1::set_status(int state) {
status = state;
}
void C2::set_status(int state) {
status = state;
}
void C1::get_status(C2 y) //Member function of C1
{
if (y.status | status)
cout << " PRINT " << endl;
}
int main() {
C1 c;
C2 d;
d.set_status(1);
c.get_status(d);
}
This compiled (without errors or warnings, at with default flags) with g++ 4.9 and VC++ 12 (aka VC++ 2013). Both produced: PRINT as output.
It's pretty common for an IDE to parse the source code separately from the actual compiler, and some of them are fairly limited compared to real compilers, so I guess it's not terribly surprising that they get confused about some things.
main:
#include <iostream>
#include <string>
#include "serviceChargeChecking.h"
int main()
{
serviceChargeChecking newAccount("Crim", 111222, 50.00, 100, 1.00);
system("PAUSE");
return 0;
}
serviceChargeChecking.h:
#ifndef H_serviceChargeChecking
#define H_serviceChargeChecking
#include "checkingaccount.h"
#include <string>
class serviceChargeChecking: public checkingAccount
{
public:
void setMonthlyFee(double);
void writeCheck(int);
void getMonthlyStatement() const;
serviceChargeChecking(std::string =" ",int = 0, double = 0.00, int= 0, double = 0.00);
private:
double serviceCharge;
};
#endif
serviceChargeChecking.cpp:
#include "serviceChargeChecking.h"
#include <iostream>
using std::string;
void serviceChargeChecking::setMonthlyFee(double fee)
{
serviceCharge=fee;
}
void serviceChargeChecking::getMonthlyStatement() const
{
checkingAccount::getMonthlyStatement();
std::cout<< "Service Charge: " << serviceCharge << std::endl;
}
void serviceChargeChecking::writeCheck(int ammount)
{
if(checkingAccount::getChecks()>0)
{
checkingAccount::setChecks(checkingAccount::getChecks()-ammount);
}
else
{
std::cout<<"No checks available." << std::endl;
}
}
serviceChargeChecking::serviceChargeChecking(string name, int acct, double bal, int numCheck, double sCharge)
{
bankAccount::setAcctOwnersName(name);
bankAccount::setAcctNum(acct);
bankAccount::setBalance(bal);
checkingAccount::setChecks(numCheck);
serviceCharge=sCharge;
}
checkingAccount.h:
#ifndef H_checkingAccount
#define H_checkingAccount
#include "bankAccount.h"
#include <iostream>
class checkingAccount: public bankAccount
{
public:
virtual void writeCheck()=0;
void deposit(double);
void withdraw(double);
void getMonthlyStatement() const;
int getChecks();
void setChecks(int);
private:
int numChecks;
};
#endif
checkingAccount.cpp:
#include "checkingAccount.h"
#include <iostream>
int checkingAccount::getChecks()
{
return numChecks;
}
void checkingAccount::setChecks(int c)
{
numChecks=c;
}
void checkingAccount::deposit(double d)
{
bankAccount::setBalance(bankAccount::getBalance()+d);
}
void checkingAccount::withdraw(double w)
{
bankAccount::setBalance(bankAccount::getBalance()-w);
}
void checkingAccount::getMonthlyStatement() const
{
bankAccount::getMonthlyStatement();
}
bankAccount.h:
#ifndef H_bankAccount
#define H_bankAccount
#include <string>
class bankAccount
{
public:
std::string getAcctOwnersName() const;
int getAcctNum() const;
double getBalance() const;
void getMonthlyStatement() const;
void setAcctOwnersName(std::string);
void setAcctNum(int);
void setBalance(double);
virtual void withdraw(double)=0;
virtual void deposit(double)=0;
private:
std::string acctOwnersName;
int acctNum;
double acctBalance;
};
#endif
bankAccount.cpp:
#include "bankAccount.h"
#include <iostream>
using std::string;
string bankAccount::getAcctOwnersName() const
{
return acctOwnersName;
}
int bankAccount::getAcctNum() const
{
return acctNum;
}
double bankAccount::getBalance() const
{
return acctBalance;
}
void bankAccount::setAcctOwnersName(string name)
{
acctOwnersName=name;
}
void bankAccount::setAcctNum(int num)
{
acctNum=num;
}
void bankAccount::setBalance(double b)
{
acctBalance=b;
}
void bankAccount::getMonthlyStatement() const
{
std::cout << "Name on Account: " << getAcctOwnersName() << std::endl;
std::cout << "Account Id: " << getAcctNum() << std::endl;
std::cout << "Balance: " << getBalance() << std::endl;
}
I know this is a lot of code to go through but can anyone help me understand why i cannot create an object from the class serviceChargeChecking the error is telling me that i cannot create an object from the abstract class but it doesn't seem to be abstract to me.
serviceChargeChecking implements void writeCheck(int), but the pure virtual function from checkingAccount has type void writeCheck(), so it's still pure in serviceChargeChecking, which makes the class abstract.
You have this in the abstract class checkingAccount:
virtual void writeCheck()=0;
but implement this in the derived class serviceChargeChecking:
void writeCheck(int);
The signature must be the same.
The writeCheck() method has different signatures in serviceChargeChecking and checkingAccount.
If you use C++11, use override in order to avoid this kind of error.
It's because your CheckingAcount has writeCheck() and serviceChargeChecking has writeCheck(int);
This probably due to the fact that you failed to Override checkingAccount's, writeCheck method, the abstract prototype was was
in checkingAccount class
virtual void writeCheck()=0;
and in serviceChargeChecking class
void writeCheck(int);
note the parameters, you didn't override checkingAccount's writeCheck you probably inherited it (implicitly), the serviceChargeChecking made a new writeCheck with an int parameter.