Hi All, i am curious to know how to set the value (value=10) through B's constructor in below programme. below is example of multiple inheritance. As construction always happens from right to left so it always call the 'C' class's constructor and set the value 20 but as per my requirement if i want to call only B class's constructor and set the value 10 it is not happening. so how does this virtual inheritance solve this problem here. #include<iostream> using namespace std; class A { int x; public: void setX(int i) {x = i;} void print() { cout << …

Member Avatar
Member Avatar
+0 forum 9

inheritance error is::Error 1 Inconsistent accessibility: base class 'person' is less accessible than class 'employee' using System; using System.Collections.Generic; using System.Linq; using System.Text; //person class public class person { public string name; public double CNIC_no; public void Information() { Console.WriteLine("Enter your name"); name = Console.ReadLine(); Console.WriteLine("Enter your id"); CNIC_no =double.Parse( Console.ReadLine()); } public void print() { Console.WriteLine( "your name is {0}",name); Console.WriteLine(" your CNIC_no is {0}",CNIC_no); } } //student class and inherit to person class student: person { string class_name; int id; public void Information_student() { Console.WriteLine("Enter your class name"); class_name = Console.ReadLine(); Console.WriteLine("Enter your id"); id = int.Parse(Console.ReadLine()); } …

Member Avatar
Member Avatar
+0 forum 2

Inheritance with generic types can sometimes be tricky. If you would inherit from a Stack<double>, no problem, but what if you want to keep it as generic as possible? Maybe it's a bad idea, but I wrote something like `class Stacker<T> : Stack<T>` and in Stacker I implemented some arithmetic methods like divide now if T is a double, all is well, but if T is a Person class? To divide two Persons would be hard to do. So our smart C# compiler does not let you use arithmetic operators here. This means T + T or T / T …

Member Avatar
Member Avatar
+1 forum 7

I have a Task class (superclass) which inherited PracticeTest class(childclass). I'd faced question regarding my pass year question. In fact, we are allowed to declared like this Task aTask = new PracticeTest(); But i wonder why we are doing this way? Thanks for advance explanation.

Member Avatar
Member Avatar
+0 forum 9

Is it possible to let an overridden method from a base class to return the type of the derived class? What I mean is: namespace test //with some "pseudo" code { class Base<T> { //fields ... //properties ... // ... public virtual Base<T> BaseMethod() { //return new Base<T>; } // ... } class DerivedfromBase : Base<int> { // ... public override Base<int> BaseMethod() { //other things here return // a Base<int> type; //Can it return DerivedfromBase type here? } // ... } // Main { DerivedfromBase AA = new DerivedfromBase(); //will compile, but run will give invalid cast exeption DerivedfromBase …

Member Avatar
Member Avatar
+0 forum 14

Hi guys, I would like to discuss my design with one query. below is the code snippet. It is clear from below code snippet that class pd is privately derived from AbstractClass as both are different classes and there is no relation between them.Now in my class pd i need ConcreteClassB's service call to proceed further for my version of do_servicecall function so i have overridden the same. Now if you see the do_servicecall functionimplementation inside pd class then you will realize that through servicecall function(defined in AbstractClass) i am able to overide the implementation provided by ConcreteClassB . Here …

Member Avatar
Member Avatar
+0 forum 9

Hi fellow members, I have wrote two abstract classes, with the second inheriting from the first. However, I receive this error when I try to compile: "error C2143: syntax error : missing ',' before '<'." Will somebody help me out? Thanks! template <class DataType> class AbstactArrayClass { friend ostream& operator << (ostream& s, AbstactArrayClass<DataType>& ac); public: virtual int size() const = NULL; virtual DataType& operator[] (int k) = NULL; }; template <class DataType> ostream& operator << (ostream& s, AbstactArrayClass<DataType>& ac) { s << "["; for (int i = 0; i < ac.size(); i++) { if (i > 0) { s …

Member Avatar
Member Avatar
+0 forum 1

//Hi all just switching from c++ to c#.Can anyone help,with the question i have: //this is a peace of code from c++ banking aplication int ShowMenu(void); void AddAccount(vector<CAccount*>& list); void DisplayAccount (vector<CAccount*>& list); void Lodgement (vector<CAccount*>& list); void Withdroawal (vector<CAccount*>& list); void InterestRate (vector<CAccount*>& list); CAccount* findAccount (vector<CAccount*>string ); bool quit = false; int _tmain(int argc, _TCHAR* argv[]) { vector<CAccount*> list; int option = 0; do{ quit =false; option =ShowMenu(); switch ( option ) { case 1: AddAccount(list); break; case 2: DisplayAccount(list); break; case 3: Lodgement(list); break; case 4: Withdroawal(list); break; case 5: InterestRate(list); break; case 6: quit = true; …

Member Avatar
Member Avatar
+0 forum 42

Why this doesn't work? class Faculty extends Employee { private long[] officeHours; private String rank; public Faculty(long[] hours, String rank) { this.officeHours = hours; this.rank = rank; } public static void main(String[] args){ //some code here Faculty f = new Faculty(newToken[], tokens[tokens.length-1]);

Member Avatar
Member Avatar
+0 forum 5

One of my students make me a question (which answer i know) but I think could be instructive to formulate the same question here so many people can learn about it. The question is: How to implement a copy assignment into a Derived class? Consider the following class A: class A { public: A(int i1_) : i1{i1_} {} A(const A& a) : i1{a.i1} {} // Copy constructor A& operator=(const A& a) // Copy assignment { i1=a.i1; return *this; } private: int i1; }; and you want to implement a derived class B: class B: public A { public: B(int i1_, …

Member Avatar
Member Avatar
+0 forum 8

Hey everybody, As of this moment i'm trying to work out how to approach a state design. I've come across Source Makers' site [http://sourcemaking.com/design_patterns/state/cpp/1](http://sourcemaking.com/design_patterns/state/cpp/1) and am testing out the example. The program works fine when it's all in 1 file, although i had to change some stuff in main, but when i separate the program into headers and .cpp errors would occur. This is what i have at the moment. There's quite a lot of errors but i think it all has to do with the "base class not being defined". The majority of errors are defined as... `error C2504: …

Member Avatar
Member Avatar
+0 forum 5

this program is simply for recording data of various staffs.Inheritance has been used for taking additional data.Staff is the base class.the classes officer,typist and teacher are the derived classes.again the classes regular and casual are derived from the class typist. **now my problem is that whenever i call the constructor functions of respective classes,it gives an error saying no match to function call.can anyone help me with this. i dont get why the call doesnt match as all the arugments match....???** #include<iostream> #include<string.h> using namespace std; class staff{ protected: char name_main[]; int code; public: staff(){} staff(char n[],int c) { code=c; …

Member Avatar
Member Avatar
+0 forum 2

I'm trying to create a program that passes arguments of size of three shapes, a circle, a triangle, and a rectangle. I got those but everytime I run the code it displays the arguments as 0 instead of giving me the shape dimension. Here is the code. using System; class TestShapeHierarchy { static void Main(string[] args) { TwoDimShape[] myShapes = new TwoDimShape[3]; myShapes[0] = new Rectangle(10, 5); myShapes[1] = new Triangle(12, 6); myShapes[2] = new Circle(8); foreach (TwoDimShape tds in myShapes) { ProcessShape(tds); Console.WriteLine(""); { } } } // you need to write a static method processShape static void ProcessShape(TwoDimShape …

Member Avatar
Member Avatar
+0 forum 1

I have two classes A Dog class whose constructor does not accept arguments, `public class Dog<pet> extends Animal<pet>{}` whose constructor does not accept any arguments and `public class Animal<pet>{}` whose constructor accepts one argument public class Dog<pet> extends Animal<pet>{ public Dog(){ super(/*what should go here?*/); //Constructor accepts no arguments } } public class Animal<pet>{ public Animal(E name){ //Constructor accepts one argument } } I need to pass an E type argument to super() inside the constructor of the Dog<pet> class. How can I do this without passing any arguments to the dog constructor? Thanks

Member Avatar
Member Avatar
+0 forum 3

please i have a little problem creating a class and making it run. The class is created separately as a header file with it's source code. whenever i build the source code of the class, i get an error that says: undefined reference to 'WinMain@16'. And whenever I run the main, it gives me the error: undefined reference to 'samclass::samclass' here's my code main #include<iostream> #include "samclass.h" using namespace std; int main(){ samclass obj; return 0;} header #ifndef SAMCLASS_H #define SAMCLASS_H #include <iostream> using namespace std; class samclass { public: samclass(); void printcrap(); }; #endif // SAMCLASS_H header source code …

Member Avatar
Member Avatar
+0 forum 3

I have been programming for quite a while in Python but have just started to learn OOP properly. I find it really confusing and I don't really understand it. Especially the implicit self that python automatically passes. It just isn't clicking, really. May someone provide an easy explanation of what classes and objects are in general and how they are used in Python?

Member Avatar
Member Avatar
+0 forum 2

package hw3.categories; import java.util.ArrayList; import hw3.DiceGroup; import hw3.api.IScoreCategory; /** * Scoring category for a yahtzee. A DiceGroup * with N dice satisfies this category only if all N * values are the same. For a dice group that satisfies * this category, the score is a fixed value specified in the constructor; * otherwise, the score is zero. */ //Note: You must edit this declaration to implement IScoreCategory //or to extend some other class that implements IScoreCategory public class YahtzeeCategory implements IScoreCategory { private String category; private int point; private ArrayList<Integer> diceSet; private DiceGroup dGroup; /** * Constructs a YahtzeeCategory …

Member Avatar
Member Avatar
+0 forum 2

This started out as `I love PHP traits! I'm going to use them everywhere! ^_^` and now it has turned into a `Thought Exercise / Learning Experience >_<`. Consider the following example: trait TheErrorOfYourWays{ public function booboo(){ echo 'You had a booboo :('; } } trait SpectacularStuff1 { use TheErrorOfYourWays; } trait SpectacularStuff2 { use TheErrorOfYourWays; } class DoSomethingSpectacular { use SpectacularStuff1, SpectacularStuff2; } This results in (obviously): > Fatal error: Trait method booboo has not been applied, because there are collisions with other trait methods on DoSomethingSpectacular. **So my question: How do I resolve method conflicts in traits? Is …

Member Avatar
Member Avatar
+2 forum 6

Hello, I am working on an event driven library and am having one minor problem. Basically I have two ways to get what I want done. Method 1: Smart pointers and factory functions #include <iostream> #include <vector> #include <memory> using namespace std; class Object { public: virtual int getInt(){return 0;} static shared_ptr<Object> create() {// of course this could be replaced with the templated version to make it work for any constructor return shared_ptr<Object>(new Object()); } }; class One:public Object { public: virtual int getInt(){return 1;} static shared_ptr<One> create() { return shared_ptr<One>(new One()); } }; class Incrementer:public Object { int x; …

Member Avatar
Member Avatar
+0 forum 4

#ifndef BASE_H #define BASE_H class Base { public: int m_nValue; Base(int nValue=0) : m_nValue(nValue) { std::cout << "Base" << std::endl; } }; #endif #ifndef DERIVED_H #define DERIVED_H #include "Base.h" class Derived: public Base { public: double m_dValue; Derived(double dValue=0.0) : m_dValue(dValue) { std::cout << "derived se" << std::endl; } friend std::ostream & operator << (std::ostream& os, const Derived &d); friend std::istream & operator >>(std::istream& is, Derived &d); }; inline std::ostream& operator << (std::ostream& os, const Derived &d) { return os << d.m_dValue; } inline std::istream & operator >>(std::istream& is, Derived &d) { return is >> d.m_dValue; } #endif #include <iostream> …

Member Avatar
Member Avatar
+0 forum 3

Hello, I am having a particularly nasty case of "What is the syntax?" while working on an event driven library. Basically this is what I want: class Object {// private: public: virtual bool onEvent(Event e)=0;//all objects have to react to events Object operator|(const Object &o) { // I want to return an Object which will execute both onEvents... // How can I do this... IF I can do this... // my example attempt: (I have yet to fully memorize the lambda expression syntax in c++11) return Object::onEvent(Event e)=[]{return (*this).onEvent(e) || o.onEvent(e);} } }; class Test1:public Object {// private: public: virtual …

Member Avatar
Member Avatar
+0 forum 7

#include <string> #include <iostream> #include <vector> using namespace std; class Person { public: Person(); Person(string n, string a, string tel, string em); string getName(); string getAddress(); string getTel(); string getEmail(); virtual string whatAmI(); private: string name, address, telephone, email; }; Person::Person() { } Person::Person(string n, string a, string tel, string em) { name = n; address = a; telephone = tel; email = em; } string Person::getName() { return name; } string Person::getAddress() { return address; } string Person::getTel() { return telephone; } string Person::getEmail() { return email; } string Person::whatAmI() { return "Person"; } //Class Student derived from Person …

Member Avatar
Member Avatar
+0 forum 4

I'm trying to create a list stack that implements some interface called SomeList The list stack should only use stack with no links. the itnerface has a number of methods that I would like to implement. So for SomeList I have something like public interface SomeList<T>{public void plus(T something);public E takeout(int somewhere); and so on} For the generic ListStack I have something like public class ListStack<T> implements SomeList<T>{ Stack<T> stack1=new Stack<E>(); //constructor public LinkStack(){ stack1=new Stack<T>(); } //method public void plus(T somethig){ this.push();} . . . } In the method above (plus(T something)) I get an error on push. Eclipse …

Member Avatar
Member Avatar
+0 forum 13

Hey guys, slightly less noob programmer here for a some more help, This week in class we were assigned to create a phonebook with a couple functions to manipulate it, but it seems I am having a couple problems. It seems to me that I have most of the program running but I do not know how to fix the last error its giving me. It says that in transducer.h when I declare Phonebook phonebook; it says im missing a ';' before phonebook... well anyways, here's the code Person.h #pragma once #include <string> #include "Phonebook.h" class Person { public: friend …

Member Avatar
Member Avatar
+0 forum 1

Hey guys i am having trouble with creating a Parent class(gameobject) for two other child classes(player,location) both of the child classes can access data from the parent class, and work well independently but once i include the h file for the child classes together in main.cpp i get the compiler error stating base class unidentified and class type redefinition is visual c++ what am i doing wrong ? sorry for the formating //BASE CLASS OR PARENT CLASS #include <iostream> #include <string> using namespace std; class GameObject { public : string gamename; string gametype; string gamesize; //default constructor GameObject() { gamename …

Member Avatar
Member Avatar
+0 forum 7

Hi Guys, I am trying to teach myself templates and I keep getting lost in all these compilation errors. Here is the latest // This is the interface for the base class template<class V, class I =int> class Array{ private: vector<V> myStore; public: Array(); Array(I size); Array(I size, V value); ~Array(); int size(); const V& element(I index); V& operator[] (I index); void print(); }; // This is the interface for the derived class template<class V, class I=int> class NumericArray: public Array<V,I>{ public: NumericArray(); NumericArray(I size){} NumericArray(int size, double value){} ~NumericArray(){} // void print(); }; This is my implementation for the …

Member Avatar
Member Avatar
+0 forum 5

Hey guys, noob programmer here. Over the last week or so we've been learning about classes and inheritance in my latest progamming class. I've recieved a variation of the ever so popular poker program and am having some problems putting it all together. I haven't had the opportunity to test the logic because the program obviously isnt in a running state. The parts I KNOW I have a problem with are dealing from the vector, I just dont know the syntax to transfer it to myHand[]. I am also having problems with calling the functions in main another syntax problem …

Member Avatar
Member Avatar
+0 forum 10

I have the following structure: CObject { protected: char *mName; public: CObject(char *n) { mName=strdup(n); } }; CVector:public CObject { char *mValues[50]; int mElements; public: CVector(char *n):CObject(n) {} }; CMatrix:public CObject { char *mValues[50][50]; int mLines; int mColumns; public: CMatrix(char *n):CObject(n) {} }; My main function: int main() { pV=new CVector("Vector 1"); pM=new CMatrice("Matrice 1"); delete pV; delete pM; } As you can see, i want to explicitly call them.

Member Avatar
Member Avatar
+0 forum 12

Hello, I have a chain of inherited classes like this: class A { protected: int a; public: int getA(){return a;} }; class B:public A { protected: int b; public: int getB(){return b;} }; Based on that you can see that the public can only read the values in those classes, they cannot write them. However I would like to make a sort of overriding class that will allow those with access to it to modify the classes, like so: class ExposedA:public A { public: ExposedA &setA(int a){this->a=a;return *this;} }; //here is where I am lost class ExposedB:public B { public: …

Member Avatar
Member Avatar
+0 forum 7

Traceback (most recent call last): File "C:/Users/James/Desktop/hugevar.py", line 2, in <module> class var(tempfile.TemporaryFile): TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str Why does this happen and how can I fix it?

Member Avatar
Member Avatar
+0 forum 2

The End.