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.
Related
Good day everyone! I would just like to ask if how it is possible to access a private structure inside a class?
My code looks like this:
class VideoClass{
private:
struct vidstruct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
};
public:
VideoClass();
// ~VideoClass();
void insertVideo(vidstruct info);
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
What I wanted to is to access the vidstruct (name of the structure) to any parts of my program. Thankyou for your kind answers in advance :)
The fact that your member vidstruct is declared as private means that it is not accessible outside of the class. If it is meant to be used outside of the class You could declare it simply outside of it.
Here would be a snippet example (printing 'movie test'):
#include <iostream>
using namespace std;
typedef struct {
int Video_ID;
string movietitle;
string genre;
string prod;
int numberOfCopies;
string MovImg_name;
} vidstruct;
class VideoClass{
public:
VideoClass() {
};
// ~VideoClass();
void insertVideo(vidstruct info) {
cout << info.movietitle;
};
void rentVideo(int rv); //
void returnVideo(); // ---
void showDetails(int sd);
void validateVideo(); //
void displayVideo();
};
int main()
{
vidstruct x;
x.movietitle = "movie test";
VideoClass videoTest;
videoTest.insertVideo(x);
return 0;
}
i was making a c++ program with class to class type conversion and i wrote this code... the variable i is not being affected by type conversion
i tried taking it outside but i cant make it work...please help
#include<conio.h>
#include<iostream>
using namespace std;
class feet
{
int f;
public:
void inputfeet()
{
cin>>f;
}
get()
{
return (f);
}
};
class inches
{
int i;
public:
void inputinches()
{
cin>>i;
}
void operator - (feet x){
i=i+((x.get())*12);
}
void show(){
cout<<"inches"<<i;
}
};
int main()
{
feet foot;
inches inch;
foot.inputfeet();
inch.inputinches();
-inch;
inch.show();
return 0;
}
I am new to C++ and using Xcode and I am having an issue,
in my main .cpp file Account.cpp the code is-
#include <iostream>
using namespace std;
#include "Account.h"
Account::Account()
{
double balance=0;
balance=0;
}
Account getbalance()
{
return balance;
}
void deposit(double amount)
{
balance+=amount;
}
void withdraw(double amount)
{
balance-=amount;
}
void addInterest(double interestRate)
{
balance=balance*(1+interestRate);
}
I think I missed something but I'm not sure where, any help would be appreciated thank you.
**The header file Account.h is-
#include <iostream>
using namespace std;
class Account
{
private:
double balance;
public:
Account();
Account(double);
double getBalance();
void deposit(double amount);
void withdraw(double amount);
void addInterest(double interestRate);
};
Write the constructor the following way
Account::Account()
{
balance = 0.0;
}
I suppose that balance is a data member of type double of class Account.
Or you can write
Account::Account() : balance( 0.0 ) {}
And all these function definitions if the functions are class member functions must look at least like
double Account::getBalance()
{
return balance;
}
void Account::deposit(double amount)
{
balance+=amount;
}
void Account::withdraw(double amount)
{
balance-=amount;
}
void Account::addInterest(double interestRate)
{
balance=balance*(1+interestRate);
}
Also it seems that you forgot to define the constructor with a parameter.
Account::Account( double initial_balance ) : balance( initial_balance ) {}
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.
I am getting an error extra qualification ‘student::’ on member ‘student’ [-fpermissive].
And also why name::name such syntax is used in constructor?
#include<iostream>
#include<string.h>
using namespace std;
class student
{
private:
int id;
char name[30];
public:
/* void read()
{
cout<<"enter id"<<endl;
cin>>id;
cout<<"enter name"<<endl;
cin>>name;
}*/
void show()
{
cout<<id<<name<<endl;
}
student::student()
{
id=0;
strcpy(name,"undefine");
}
};
main()
{
student s1;
// s1.read();
cout<<"showing data of s1"<<endl;
s1.show();
// s2.read();
//cout<<"showing data of s2"<<endl;
//s2.show();
}
In-class definitions of member function(s)/constructor(s)/destructor don't require qualification such as student::.
So this code,
student::student()
{
id=0;
strcpy(name,"undefine");
}
should be this:
student()
{
id=0;
strcpy(name,"undefine");
}
The qualification student:: is required only if you define the member functions outside the class, usually in .cpp file.
It would be correct if definition of constructor would appear outside of class definition.
Similar code:
Change from:
class Solution {
public:
static int Solution::curr_h; // ----------------- ISSUE: "Solution::" is extra
static int Solution::curr_m; // ----------------- ISSUE: "Solution::" is extra
};
int Solution::curr_h = 0;
int Solution::curr_m = 0;
to:
class Solution {
public:
static int curr_h; // ----------------- FIX: remove "Solution::"
static int curr_m; // ----------------- FIX: remove "Solution::"
};
int Solution::curr_h = 0; // <------ good here - "Solution::" is required
int Solution::curr_m = 0; // <------ good here - "Solution::" is required
This happens because copy pasting statics require initialization outside of class, but also requires all the declaration (int) too.
Though it is correct, wish there is a easier/ better way to do the same.