C/C++ :: Implementing Container Class That Holds Point Class
Nov 28, 2014
I have a Point class that's already implemented. My goal is to implement a container class called Line that holds the Point class. I'm not allowed to use any existing container classes for this. Here's what I'm working with:
//File: Point.h
#ifndef POINT_H_
#define POINT_H_
class Point {
public:
Point(int x = 0, int y = 0);
Point(const Point & t);
virtual ~Point() {};
[Code] ....
How I'm supposed to write Line.cpp...how do I access/add Points to Line without using something like a vector? I probably should've included what I've written so far.
#include "Line.h"
/**
* Default Line constructor
*/
Line::Line() {
Lets say we have a class that holds a pointer member to another object. If I delete that pointer in the destructor I get an error (and I understand why). My question is : is it possible to overcome that without memory leaks ?
1 #include<iostream> 2 using namespace std; 3 4 class A { 5 public: 6 ~A() { 7 cout<< "~A()" <<endl;
#include <iostream> using namespace std; class superclass; class subclass1; class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
The following program is designed to demonstrate class templates. The program will evaluate postfix expressions input to the console by pushing them in a stack, and then popping them and evaluating them as such, (ex. entering 3 4 + would equal 3+4 = 7).
The code is below. We are not to modify it, but to fill in the blanks, the places filled in indicated with two asterisks for a line, and one on each side for a part of a line. If I didn't know what to enter (if anything), I put three ?s. If you want to copy and compile for yourself, look for all the *s and ?s.
1) I'm turning up all sorts of errors in the main program file (prog5.cpp) having to do with stacktype.cpp. It has been removed from the program, as it is included at the end of stackType.h. Most of them are "cannot convert 'this' pointer from StackType to StackType<stack> &'. How do I fix that?
2) The program supposedly lacks a default constructor, and it keeps turning up that 's' is an array of unknown size (do I call StackType or stack or what?).
stackType.h Code: #pragma once// Catherine Stringfellow and Trey Brumley // A Stack is a data type, which stores values in an order where values are added to the top of the stack when pushed, // and when popped, remove and return the value from the top of the stack. // Class specification for Stack ADT in file StackType.h using namespace std; static const int MAXITEMS = 50;
So this code compiles without any problem but it is not producing the correct output. I know there's a problem in either my getBlock or putBlock functions but I can't see it.
Currently the output is "Should be 32 1s: " "Should be 32 2s: "
There should be 32 1s and 32 2s and nothing is coming out.
#include <iostream> #include <fstream> using namespace std; class Sdisk { public : Sdisk(string diskname);
For class we are required to implement a signature block on all our assignments. To do this I've created a Signature Block class, but I'm having trouble implementing it. When I try to compile in Dev C++ I get this error:
[Error] request for member 'toString' in 'myblock', which is of non-class type 'SignatureBlock()'
Here is the code:
Assignment1.cpp
#include <iostream> #include <string> #include "SignatureBlock.h" // Assignment 1: Requests user's name and says "Hello." using namespace std; int main(int argc, char** argv) { string name;// string to store user's name SignatureBlock myblock();// create a signature block object
How would I use the list container to hold a class?
class A { private : int x; public : void setX(int val) { x = val; } };
class B { private : std::list<A> pdata; public : void addToList(); };
For adding, I thought of trying something like
void B::addToList() { A *tmp = new A; if(A != 0) { tmp->setX(5); pdata.insert(tmp); delete tmp; } }
How would I do what I'm trying to do? Or is this the wrong way to go about it? For the actual program, "B" would contain several lists of various classes.
class A (abstract) class B : A class C { void add ( A(&*?) a ) std::vector<std::unique_ptr<A>> data; //unique_ptr<A> because A is abstract and therefore vector<A> isn't possible }
upper situation. What is the best way to pass add an object of class B to C?
with C::add(A* a){ vector.push_back( unique_ptr<A>(a) ); } and int main() { C c; c.add( new B() ); }
This works, but i don't think it's very nice, because you could delete the pointer in main. What happens then with the unique_ptr? I could probably make C::add( std::unique_ptr<A> u_p ); but maybe it can be avoided that the "user" (in main() ) has to create the unique_ptr itself.
What is the result type? Obviously, it's up to me to decide this. As reference, consider the type promotion rules for native types:
Code: short a; int b; int result = a + b; In this case, the short value is promoted to the int value, and the addition happens on int.
It would seem a similar rule (go to the wider type) would be appropriate for fixed point. But there is another dimension to the problem, which is the number of fraction bits. Should you go to the wider type? Or the most precise type? Should you endeavor to minimize the number of bits which are discarded? What's the most intuitive rule?
I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.
For exemple:
Code: template <class T> class A { public: A() { version = ???? } std::string_base<T> version; };
I don't want to pass the default value as parameter of the constructor. how I can do this?
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight() fight(Player p1, Player p2) { p1.func2(); } //using function fight() fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
#include <iostream> #include "curve1.h" #include "curve2.h" using namespace std; int main() { Curve1 curve1Obj; Curve2 curve2Obj;
[Code]...
Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.
My base class header score.h looks like this:
#ifndef SCORE_H #define SCORE_H class Score { private: int *ipScore; float fAverage; int iSize;
[Code]...
You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously). .....
The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....
How can I cause it to return the old values that are set in curve1.cpp?
Here is my curve2.cpp
#include <iostream> #include "curve2.h" using namespace std; void Curve2::curve() { cout << "getSize() returns: " << getSize() << endl; // out comes the garbage }
Can I use a function to simply put values from old to new variables? If yes then how?
Code: Error1error LNK2019: unresolved external symbol "public: __thiscall ReachTop<class Character>::ReachTop<class Character>(class Character *)" (??0?$ReachTop@VCharacter@@@@QAE@PAVCharacter@@@Z) referenced in function "void __cdecl `dynamic initializer for 'gReachTop''(void)" (??__EgReachTop@@YAXXZ)Main.objDecisionTest
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code: class B { public: B():_y(1){} virtual ~B() { cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
Let's say I have a Car object , and it contains inner Engine object.
Code: struct Car{ Engine mEngine; };
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code: Car::Car: mEngin(arg1,arg2,...) { other stuff here }
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code: class Toyota: Car(...), mEngine(...), mGear(..) { ... };
The other options are: 1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive 2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;
it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:
std::vector<base> Vec;
then I add an element into it like this
Derived DerElement; Vec.push_back(DerElement);
then when I type:
for (int i=0; i<Vec.size(); i++) { Vec.at(i).Update(); }
It outputs:
Derived DerElement2; DerElement2.Update();
and it outputs this:
#include <iostream> #include <vector> class Base { public: virtual void Update() {
I have an example where I have a variable belonging to a base class, but I would like to tell the compiler that it actually belongs to a derived class. How can I do this?
// base class: R0 // derived class: R1 // see function SetR1 for the problem class R0 { public: int a;