C# :: Using Different Instance Of Class Depending On User Input?
Jan 13, 2015
Here's the problem I want to intialise different classes based on user input. I've tried using an IF statement but the scope of an if statement would mean the rest main program wouldn't be able to see it.
I'm trying to get this program to loop the number of times I get it to input. The program compiles alright, and it does loop when I tell it too, but how do I output the grades of the multiple students?
#include <fstream> #include <iostream> #include <iomanip> #include <string> #include <cstdlib> int weighted1 = 0; int weighted2 = 0;
I am creating a program that allows a user to create multiple 'sequences' and multiple 'filters' and then apply a filter to a sequence.
Each sequence and filter is an array of values.
How do I go about allowing the user to create a 'new' sequence and then store the location so I can access it again later? Once they have created a new sequence they can go on to create another sequence and ten maybe a filter and then another sequence etc etc .. and then they can select sequence 1 and edit the values if they so wish.
They would be asked how many sample values for the sequence, and then I would create a sequence with that many values and an id (1,2,3,4...). They could then enter this id to view/edit the sequence.
The entering/editing values part I am fine with. I just don't know how to allow them to create multiple new instances of a class without using an array so something like..
sequenceClassName somearray[10]; int i; *create a new array* somearray[i].create_class(how_many_samples) i++ //so next sequence they create is 2,3,4.. etc
- this then calls the member function that creates an array using 'sample_values = new float[how_many_samples]' and the user can input their data and edit it whenever by entering the id which will correspond to the somearray[i].
However that approach only allows them to enter a maximum of 11 sequences. It all depends on how big I make that initial array and it just seems like the wrong way to do it.
( how to interact with them, just how to create multiple classes and recall them later to access the data!)
For example, to calculate GPA, we add all the grade point of a course and divide by the number of courses we are taking. How do I create a program in such a way that when the user enters grade point for two courses, it will add the grade points for the two course and divide by 2. And if another user enters grade points for six courses, it adds the grade points and divides by 6.
Below is a program that generates random Addition for Subtraction problems depending on the user's choice. The user is prompted to input an answer and then it keeps your score. If you want to quit you just press zero.
Code: #include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <iomanip> #include <string> using namespace std; int menu(); bool addition(int &reward); // function to return truth values
I am trying to simplify my code by creating a function which takes an input then checks whether it is "y" or "n" then outputs either 1 or 0 depending on the input. So far this is what I have
int Choice(string choice) { while(choice.compare("n") != 0 && choice.compare("N") != 0 && choice.compare("y") != 0 && choice.compare("Y") != 0){ cout << "Please enter a valid input either [y/n] : " << endl; cin.clear(); cin.ignore(); cin >> choice;
[Code] ...
And I call it in the program using
cout << "Do you wish to change the hubble type of any galaxies? [y/n]" << endl; cin >> choice; while(Choice(choice) == 1){ .... cout << "Do you wish to change the hubble type of another galaxy? [y/n]" << endl; cin << choice; }
It compiles fine but displays some bizarre behaviour, I need to end the line twice in order for the program to continue and sometimes it just stops working (doesn't exit just appears to be stuck in a loop).
is there a way to have a template class respond to missing stuff in a template type ?
Code: template <typename Type> class MyClass { public: enum { ID = Type::ID }; // revert to 1 if Type::ID doesn't exist. };
If the Type passed to the template has an ID member (required to be an enum or a static const int), use it, if it is missing revert to a default value.
I can use this as a simplified way of configuring how MyClass works, without requiring Type to explicitely needing to define what it doesn't care about.
It needs to be resolved at compiletime, as it determines the number of elements in member array variables.
I have `MainShop`(base class) then `SwordShop` and `BowShop`(derived classes) and another derived class called `Inventory`. I have a vector under protected in my base class, and when I access it from my derived classes it's okay, but each one has its own value. How can I set them all to have the same value?
//MainShop.h #pragma once class MainShop { private: //some variables protected: vector <string> WeaponInventory;
[code]......
Here every time I run it, it goes straight to the `else` and it displays that I do not have any items. I want to just have one vector for both my bow and Sword shops because vectors take up a lot of memory and my teacher said keep them to a minimum. So I just want one vector that takes in items from my bow class as well as my sword class, but for some reason it's acting as if I have multiple vectors, each with its own set of items.
I am facing a real-life problem, it can be simplified as below:
#include <iostream> using namespace std; class B; class A { public: void f1(A a) {} void f2(B b) {}
[Code]...
There is no problem at all with the f1(), it compiles and executes without any problem. But f2() gives compilation error. How to solve this?
The error message is: error: 'b' has incomplete type This is just to define the function f2() in a class, that uses an instance of its child class as one of its arguments.
I am putting a instance o the Vehicle Class inside the constructor of the Calculate Class then calling it later. I get a warning saying the variable is not used and a error when I try to used the functions from the vehicle class saying use of undeclared identifier.
Okay so I have a class Student, which takes a number and a vector as a parameter for the constructor. Everything works well, until I output the values of the vector for every instance. The problem is that the same vector is being shared with EVERY instance I create, but I want it to be unique for every single one!
I was wondering if (in C++) you can instantiate a class (class foo) then have said class return the already instantiated object. (foo::instance())
In other words, can I have a class return it's-self via it's own methods? I want to be able to create a class (i.e. class foo) early in my program so it is already setup and ready to go. Then, farther down the line, I want to be able to call functions from that class without having to pass that object as an argument to my calling function. Can I do something like so:
MyClass::ReturnSelf()->foo(); or MyClass::ReturnSelf().foo();
I know that it is possible to pass a class instance to a function, but in my experience, if said function changes any variables of the class, they don't actually get changed. For example, we have class object, that has a member int number = 5. Lets say we have two functions, func1() and func2, which are not members of class object. If we pass object to func1() which, lets say, increases number by 5 (so now number = 10), at the end of that function number still = 5.
Is there a way to bypass this and have functions alter class variables permanently?
I know that I can pass variables by reference, but, in my experience, such a thing does not work with vectors (which I am also dealing with), so simple passing the desired variables by reference won't work.
I'm trying to learn as much C++ as I can. I was writing a program that mixes linked lists and classes. There is the class "Obj" which only holds an integer called 'data' and the classic "struct node" structure for linked list, but this time the "node" structure will hold an instance of "Obj" Class and the next* pointer.
#include <iostream> using namespace std; class Obj { private: int data; public:
class card { public: int id; int val; }; card card1; card1.id = 1; card1.val = 2; card card2; card2.id = 2; card2.val = 45;
etc...
So my question is firstly, is there a better way to implement this? (a vector of classes or something maybe?) and how can I call up a specific instance of the class. For example, if I want the val of a specific instance of the class, how best can I do that?
I'm trying to change the values of some instance variables in my Controller Class so that when the user inserts values into main class it changes for Controller.
class Controller { public: Controller(); ~Controller(); double PCFreq; __int64 CounterStart;
[Code] ....
The user should be able to choose which foo the want to use. So I create an object of controller in main like this
Controller* con = new Controller()
Now my issues is, when I take user input (an integer) and try to do this
con->choice1 = choice1;
only the object of con's choice1 is = to user input.
However back at the class for Controller, choice1 hasn't received a value.
I can't initialize through Controllers constructor because I get the user input through a switch statement and the range of con would only be as far as the case.
I have a class that is a template, I have to declare it in my main but i want the user to choose what type of data they will use in the class, I cant just declare myclass my, i have to use myclass<int> my, how can I change so user can select the proper datatype to use in the class.
I need to create an object of a mfc derived CFormView class that's not in the doc/template (a second view class). But it was generated with a protected ctor. Here's the code explanation with comments.
I'm thinking all the normal classes of the Doc/View template are created starting with this code, but within the template code base.
Code:
CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME, RUNTIME_CLASS(CViewSwitchDoc), //<-expands to-> ((CRuntimeClass*)(&CViewSwitchDoc::classCViewSwitchDoc)), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CViewSwitchView));
But I have generated "another view" using the "Add Class" Wizard, it's a derived class of mfc CFormView which I named ViewForm. However I'm having a problem creating an instance of it because of the generated protected ctor and pulls a compile error of not being able to access ctor. Below are the header and implementation files of this said ViewForm class. How to create an object of this view ? Did I go about it all the wrong way since it's not in the doc/template group ?
// ViewForm.h file #pragma once // ViewForm form view class ViewForm : public CFormView { DECLARE_DYNCREATE(ViewForm)
I'll just tell you in short how my Problem looks like: I should implement a New Class in a SourceCode i didnt write myself. The source code is extremely sized (i think approx >100.000 Lines of Code), so i dont want to change too much in it in order to get my Implementation done.
MY problem looks simplicified like that: Starting from 3 classes and my new class the pseudo-code looks like that:
So, i need Parameters from 3 different classes to insert in my NewClass. The 3 Classes dont know anyting about each other. So, i need to implement a Class-Instance from Type NewClass which is known by the other 3 Classes. I did solve it in this way:
I just wrote a headerfile with a class-instance which is getting included by the other 3 Classes. So they all know the same Instance and writing their Parameters into it. Is this a decent solution or could it happen to get bugs/ logical mistakes with it?
Below code produces run-time segmentation fault (core dumped) when execution reached line 53: delete [] array
Code 1:
#include <cstdlib> #include <iostream> using namespace std; #define QUANTITY 5 class Parent { protected: int ID;
[Code] ....
Output of code 1:
Constructor of A: Instance created with ID=1804289383 Constructor of A: Instance created with ID=846930886 Constructor of A: Instance created with ID=1681692777 Constructor of A: Instance created with ID=1714636915 Constructor of A: Instance created with ID=1957747793 A::showID() -- ID is 1804289383 A::showID() -- ID is 846930886 A::showID() -- ID is 1681692777 A::showID() -- ID is 1714636915 A::showID() -- ID is 1957747793 Try to delete [] array.. Segmentation fault (core dumped)
Question: Why does segmentation fault happen in code 1 above?