C++ :: Making A Money Bag - Class Inheritance
Feb 18, 2015
I am having some serious issues with class inheritance. I am trying to make a MoneyBag class inherit from a class called bag. This will not work. I get an error complaining: error: expected class-name before '{' token. And yes I have googleing it and tried several of the various solutions offered with no avail.
The MoneyBag is pretty simple right now as I wanted to get it connected to bag before I tried to do anything with it.
//MoneyBag.h//
#ifndef MONEYBAG_H
#define MONEYBAG_H
#include <bag.h>
class MoneyBag : public bag{ ////<<------ Error appears on this line.
[Code] ....
So based on everything I have seen on line the statement:
class MoneyBag : public bag{ is legal.
As it is done this way on this very site's tutorial:
class Rectangle: public Shape, public PaintCost{
View 2 Replies
ADVERTISEMENT
Jun 6, 2012
Shape base class, line and Point derived classes. What should I declare in .h files and implement in .cpp files that this is array will be work.
My major concern refer to operator [] and assign (=) operator. As far as I understand I should overload ([]) and (=) three times for classes shape , line and point or not... or is it possible made through virtual function? How will be code looks like ?
Code:
// part of main.cpp
Shape* shapes[3]; // Array of pointers to Shape
shapes[0] = new Shape();
shapes[1] = new Line ("line from array ", Point(1,22),Point(33,22));
shapes[2] = new Point(11,44);
cout << "using ToString function" << endl;
for(int i=0; i < 3; i++)
cout << s[i]->ToString();
for(i=0; i < 3; i++)
delete s[i];
View 2 Replies
View Related
Sep 16, 2014
I have couple of objects which are using some amount of methods. Right now my application is not OOP and i am going to transfer it to OOP. So i would create each class for my each object. Almost all methods are in use for each object, the only one thing which is changing some of those objects passing not all parameters inside specific method. I can go two ways one is prepare interface for all methods i got and each of my classes could implement its own definition for it but other way almost all would implement exactly the same methods but with different parameters so maybe its better to create base class then for each object do inheritance from base class (polymorphism). inside base class i can prepare base methods and classes which will inherit from that class would override those methods for requirements they want.
So imagine: You got:
Memory
CPU
Latency
and all of them using mostly all of those same methods (only arguments for one of them could be use different way):
ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()
Now its better to do like this e.g:
Base class: Stuff
prop: name, id, date ...
methods to ovveride: ExecuteQuery(), ExportToExcel() ...
classes: CPU, Memory, Latency (inheriting from Stuff)
ovveride methods and align its definition for specific class use (remember mostly only passing args are used or not by specific class)
or go with inheritance
Interface SomeMethods
ExecuteQuery()
ExportToExcel()
PopulateDataTable()
PutValueToReport()
and each class would implemet its own definition.
The most important thing is that those every objects mostly using all of those methods and what is diffrence that one object can use all of available parameters inside this method and other one no. What i should do? Go with interface or inheritance and polymporfizm inside base class?
View 2 Replies
View Related
Sep 19, 2014
I've been working on a program that uses a reference counting class that I've written which works fine for objects that inherit from it however I now need to have the following setup:
class SBComponent : private Refcounted { /*stuff*/}
class ProxiedComponent : public SBComponent, private Refcounted {/*stuff*/}
The compiler gives the following warnings
warning: direct base ‘Refcounted’ inaccessible in ‘ProxiedComponent’ due to ambiguity
And then several repeats of the error:
error: request for member ‘bk’ is ambiguous
Back *b = bk<ProxiedComponent>();
bk is a templated function that's defined in Refcounted and returns a pointer of type <template arg>::Back (ProxiedComponent::Back in this case).
I don't understand why the call to bk is ambiguous as although there's two instances of Refcounted (there needs to be with the way I've designed it so I can't make it virtual) it's inheritance is private in both cases so there should only be one instance of it visible in ProxiedComponent.
View 7 Replies
View Related
Jan 16, 2014
I have a simple question about inheritance. Consider the following code:
Code:
Class Base {
int type;
Base(){};
};
Class Derived1 : public Base
[Code] ....
I get the following error: Class "Base" has no member "Function1";
That makes sense - as Base has not declared Function1. But how can I loop through a vector of Bases, and then if the object is of type Derived1, call the function Function1?
View 11 Replies
View Related
Feb 27, 2014
I'm trying to implement a decision tree that gets doubles as input (in this sample code just random numbers) and returns a boolean value. At the nodes, I'd like to use various operators that can have different input and return types. I've implemented an abstract base class for the nodes and I'm deriving the specific nodes from it. In the code below, I've included only a few derived classes. Building the tree (randomly) is no problem. However, I'm looking for a clever way to evaluate the tree. I think that uncommenting the lines in bold print would in principle do it. However, this is not possible because "value" is not a member of the base class. The type of "value" is different in the derived classes, so I cannot simply declare it in the base class.
"Node.h"
#pragma once
class NodeBase{
public:
NodeBase* Child_1;
NodeBase* Child_2;
virtual void evaluate() = 0;
[Code] ....
View 4 Replies
View Related
Sep 16, 2014
I am trying to create a few subclasses which all use the base class constructor, according to my book this is all fine and dandy by using the "using Baseclass::Baseclass", this doesnt work for me.
class Monster {
public:
Monster(char[], char[], char[], int); //The constructor, and its implemented.
etc..
};
[Code] ....
Yet, i recieve this error:
error LNK2019: unresolved external symbol "public: __thiscall Human::Human(char * const,char * const,char * const,int)"
Why is this? Does doing it this way create const pointers somehow? The constructor works fine with Monster, i am using the same calls, just changed to create Human instead of Monster. All files are included where they should, monster and human are declared in the same header.
View 18 Replies
View Related
Nov 10, 2014
In this assignment the student should develop a month calendar by designing a class called calendarType . This class uses two other classes (dateType and dayType) as described below:
1. dayType Class: This class has been designed by students in Lab1 exercises. Referee to it.
2. dateType Class: This class is designed and implemented to keep track of data. This class has been provided it to you. Study it first then add definitions and implementations of the following operations to this class:
- Test whether the year is a leap year. Leap year is identified by 3 criteria :
- The year is evenly divisible by 4;
- If the year can be evenly divided by 100, it is NOT a leap year, unless;
- The year is also evenly divisible by 400. Then it is a leap year.
- Return the number of days in the month. For example, if the date is 12/3/2014, the number of days to be returned is 31 because there are 31 days in March.
- Return the number of days passed in the year. For example, if the date is 18/3/2014 the number of days passed is 77. Note that the number of days returned also includes the current day.
- Return the number of days remaining in the year. For example, if the date is
18/3/2014 , the number of days remaining in the year is 288.
- Calculate the new date by adding a fixed number of days to the date. For example, if the date is 18/3/2014 and the days to be added are 25, the new date is 12/4/2014.
To print monthly calendar using calendarType class, you must know the first day of the month and the number of the days in that month. Thus, you must store the first day of the month, which is in the form of dayType and the month and the year of the calendar. Clearly, the month and the year can be stored as an object of the form dateType by setting the day component of the date to 1, and the month and year as specified by user.
Design the class calendarType so that the program print a calendar for any month starting 1/1/ 1500 which is Monday. To calculate the first day of a month, you can add the 2 appropriate days to Monday of January 1, 1500. For this class identify and implement the following operations:
- Determine the first day of the month for which the calendar will be printed. Call this operation firstDayOfMonth.
- set/get month.
- set/get year.
- Print calendar for particular month.
- Add the appropriate constructors to initialize the member variables.
- Write a test program to print the calendar for either a particular year or a particular month.
View 1 Replies
View Related
Apr 19, 2014
I'm trying to make an templated class of List and trying to make a list of Students(another class) which I made. Its not working.
Code seems to work fine for pre-defined data types but it crashes when it comes to students. I also want to run sort function on list of students.
View 1 Replies
View Related
Mar 25, 2013
I'm trying to make a class constructor within a namespace and I keep getting errors like: "'<variable>' is a nonstatic data member of class '<class>'" for when I try to setup parameters, and "Incomplete type is not allow" whenever I try to write out my function definition. Here's what I'm doing:
namespace test {
class blah;
} class blah {
typedef int var[5];
[Code] .....
Also I'm unsure why there is a parameter of 'const blah &' when I mouse over blah(); (using Visual Studio 2010) within the class definition. It tells me 'blah::blah(const blah &)' and I am unsure where the parameter comes from. How can I resolve these issues?
View 4 Replies
View Related
Oct 6, 2013
I am making a simple program that is suppose to make a list of champions and their items from the game League of Legends. I am stuck on making a vector of the class so each slot within the vector would hold each champion and its data. This is what I got:
Champion_Info.h
#ifndef CHAMPION_INFO_H_INCLUDED
#define CHAMPION_INFO_H_INCLUDED
#include <vector>
#include <string>
using namespace std;
class Champ_Info
[Code] ....
View 3 Replies
View Related
Oct 31, 2013
I have a .cpp file which contains 5 smaller defined classes. In my missile class I have a default constructor and a constructor that I invoke
class Missile{
private:
bool isHuman;
[Code]...
My issue is when creating and adding the pointer object; it doesn't seem to create a new instance of the class-the Missile objects all share the same xPos value which is always the first xPos when the "fire" command is given. The "Missile *missile = new Missile(xPos, yPos, true);" line does not seem to create a new instance of the object with different variables but instead creates a new object with the same variables. Is it necessary for me to always make a separate .cpp and .h file for any class I want to create multiple instances of or can I keep the smaller classes in the same file and still create a new separate instance of the class?
View 3 Replies
View Related
Sep 26, 2013
Given any amount of money expressed in dollars, and cents, this program computes the number of 100 , 50, 20, 10, 5 and 1 dollar bills and number of quarters, dimes, nickels, and pennies to be returned, returning how many of each denomination are included.
How to program it correctly for the most part except I cannot figure out how to handle large numbers, such as 14.49999999999999999999999999999999.
When converting that number to int by multiplying by 100 it returns it as 1450 not 1449. How to handle converting doubles, similar to the one above, to an integer correctly?
This is the code that my teacher did not want, since I handled the input as a string:
#include <iostream>
#include <string>
#include<iomanip>
#include<windows.h>
using namespace std;
[Code] .....
View 5 Replies
View Related
Jul 20, 2013
I'm trying to make a very simple game in SFML, and i have a problem. Whenever i try to create sf::Sprite for the class where all properties of Player (his sprite, health, speed, etc.) It gives me error.
#include <sfml.h>
#include <list>
class Playerclass{
public:
int xspeed, yspeed;
float health;
sf::Sprite entsprite();
[Code] ....
The error is with player.entsprite.setTexture(texture) : "'player.Playerclass::entsprite' does not have class type".
View 2 Replies
View Related
Sep 26, 2013
Problem:Given any amount of money expressed in dollars, and cents, this program computes the number of 100 , 50, 20, 10, 5 and 1 dollar bills and number of quarters, dimes, nickels, and pennies to be returned, returning how many of each denomination are included.
I figured out how to program it correctly for the most part except I cannot figure out how to handle large numbers, such as 14.49999999999999999999999999999999.When converting that number to int it returns it as 14.50. How to handle converting doubles, similar to the one above, to an integer correctly?
This is the code that my teacher did not want, since I handled the input as a string:
Code:
#include <iostream>
#include <string>
#include<iomanip>
#include<windows.h>
using namespace std;
int main(){
const int hundredsConst=10000, fiftiesConst=5000, twentiesConst=2000, tensConst=1000;
[code].....
View 3 Replies
View Related
Aug 12, 2013
I've created a program meant for submission for my final project but when i ran it, it shows that the variable being used without being initialized for quite a few time. My program is below.
#include<stdio.h>
#include<conio.h>
void Kahui(float dollar, char choice);
void Qixiang(float hours, float rate);
void main(void) {
float dollars;
[Code] ....
View 2 Replies
View Related
Feb 8, 2014
I have been given to do a simple ATM project which is containing of getting a number of notes in the ATM (100, 500, 1000). Get the withdrawal of money and show the number of note. Here what i got so far. I may need to use a loop but I am not sure how to put it into this.
#include <stdio.h>
#include <conio.h>
int main() {
int a=10, b=10, c=10;
int q;
[Code] ....
But the problem is i don't know how to make the code continue with the process with the amount of notes that is left from the previous input. The program will just stop but i really want it to be able to input new withdrawal amount so that it can decrease the note each time i withdrawal the money.
View 1 Replies
View Related
Feb 20, 2013
My coin/money change code works when there can be an exact change each time, i.e. when the 1 cent option is available. However, when the change options are only $10, $5, $1, 25 cents and 10 cents, it does not give me what I want for instance, I wanted to get change for $237.80, I was expecting to get:
23 10's, one 5, two 1's and 8 dimes. However, the code below is giving me 23 10's, one 5, two 1's and 3 quarters (there is no option left for the 5 remaining cents).how to fix it?
Code:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void change(double cents, int a[]);
int main() {
double Dollars;
double cents;
[code]...
View 14 Replies
View Related
Sep 5, 2013
i have read that inheritance slows down the process.because a_class -> b_class -> c_class it goes thorough many classes to find a function or a value. people advices not to do it more than a few times. what if instead of inheritance we are using headers, would it slow down the process also??i mean which one is faster? and arent both the same when we use headers or used inheritance.
View 3 Replies
View Related
Jan 17, 2014
I want to define a base class A which allows different type of logging (file stream, console, etc). In the meantime, I want a class B to to be able to call A's print function (i.e. derived class C's print function which defines the logging method). However, I don't want to make A's print function to be public because it will only be called by B.
class A {
protected:
virtual void print(void);
};
[Code] .....
View 1 Replies
View Related
Feb 19, 2014
How to put virtual keyword in the function of the base class. Will the function still be over-written properly? What will happen if I
(1) call function from base class pointer
(2) call function from derived class pointer
(3) call function from derived class object
If the virtual keyword is present, the over-written version will be called in all cases, am I mistaken?
View 1 Replies
View Related
Feb 18, 2015
I have 2 classes with a Function with the same definition (both inherited from the same base class) and in my derived class I derive from both of those 2. Is it possible to use the Methods of both classes? for example with an other name?
class A {
protected:
int print(int a) { std::cout << "int A: " << a << std::endl; }
};
class B : A
[Code] ....
is there something like using C::print as printc;?
The Problem, I have a Sprite class that derives from a Rectangle with properties Position, Size, Origin and Angle and a Text class that derives from Rectangle. Now i have a Button class deriving from both Sprite and Text.
- The Position, when moving the Button i have to change the position of both so i Implemented a new Method which calls SetPosition from the Sprite and the Text.
- The SetSize just affects the Button so i just did using Sprite::SetSize;
- The angle affects both so i just implemented a new Method and hide the other two
The problem is here:
- The Origin: writing button.SetOrigin(-1,0) should set the Origin of the Button and writing button.SetTextOrigin should set the Origin of the text.
Should i just reimplement a Mehtod named SetTextOrigin and call Text::SetOrigin from there and hide the button.Text::SetOrigin or is there something like using Text::SetOrigin as SetTextOrigin?
View 6 Replies
View Related
May 16, 2013
#include <iostream>
using std::cout;
using std::endl;
class CBox // Base class definition
{
public:
// Base class constructor
explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) : m_Length(lv), m_Width(wv), m_Height(hv)
[Code] .....
This example produces the following output:
// Derived class copy constructor
CCandyBox(const CCandyBox& initCB): CBox(initCB) {
std::cout << std::endl << "CCandyBox copy constructor called";
// Get new memory
m_Contents = new char[ strlen(initCB.m_Contents) + 1 ];
// Copy string
strcpy_s(m_Contents, strlen(initCB.m_Contents) + 1, initCB.m_Contents);
}
It will work right? Cause when I do "CBox(initCB)" it only sends the BASE part of the derived object to the base class copy constructor, is it copy or default?
View 6 Replies
View Related
Dec 20, 2013
I have Class A as a base class , and Class B , C derived classes from A and there's class D who have a data member (pointer to Array) of type A (Composition)
class D{
A **a;
int size;
.......
a = new A*[size];
......
};
And i have Print method , in its body i have to specific element (if it from class B or C ) with a given ID(both B and C have a data member ID ) there should be 2 options in print function .. printing elements for class B , or printing elements for class C ? how can i specific the elements ?
View 5 Replies
View Related
Oct 20, 2014
I have been reading up about object oriented programming recently and have come across 'Encapsulation, Polymorphism and Inheritance' as i understand it so far all OOP programs should use these three concepts. So i started thinking how do i get these concepts into my program as i am using WPF C# and i could not really find much good info about how these concepts apply to WPF programs.
Or do these concepts just not work with WPF programs?
View 5 Replies
View Related
Feb 23, 2014
I have two Form Employee and Instructor, here Employee is super class of instructor.
public class Instructor: Employee
{
//Instructor class
}
I am able to access all property of Employee inside instructor class but with these some textbox and button design inside Employee design is also getting inherited inside Instructor design and i don want this and i want to maintain the parent-child relationship between Employee and Instructor.
View 11 Replies
View Related