It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a homework to do, I need to create three classes Student, Profesor what inherits from a class list of faculty and I dont know how to chain all OOP functionality (and must), I need to use templates and not a builtin list I need to alocate dynamic list of objects of students and profesors order by departament, I need to use static virtual methods namespace and operators rewrite, why I will need usage of virtual metods there? And usage of namespace?
I started this, but I'm in trouble, how to get a good structure to use all of the OOP functionality?
Thanks for tips!
Skiny header sourcecode:
ifndef FACULTATE_H
#define FACULTATE_H
class Facultate
{
char *nume;
list<Profesor*> profesori;
list<Student*> studenti;
public:
void addProfesor();
void addStudent();
Facultate();
virtual ~Facultate();
Facultate(const Facultate& other);
Facultate& operator=(const Facultate& other);
ostream& operator<<(ostream& O, const Facultate &F);
protected:
private:
};
#endif // FACULTATE_H
There are many ways to organize this.
Here is one layout:
class Person;
class Student : public Person;
class Facultate : public Person;
class Teacher : public Facultate;
class Staff : public Facultate;
The Person class would contain attributes common to Students and Teachers, such as first and last name.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Which is the best way to implement several order relations for only one class? I have an idea with the Strategy Pattern but I'm not sure that's a good idea. And if there is not a best way, why?
Create a functor class and initialize a member with the order relation you want to use. Have operator() use the member to decide the ordering of the two operands.
class Ordering
{
public:
Ordering(int method) : _method(method) {}
bool operator()(const MyObject & first, const MoObject & second) const
{
switch(_method)
{
case 0:
return first.name < second.name;
case 1:
return first.age < second.age;
// ...
}
}
int _method; // an enum would be better
};
std::sort(myobjs.begin(), myobjs.end(), Ordering(selected_method));
I think Strategy is a better way here, and I'm not pretty sure that a switch structure is a good idea (imagine, 1000 comparison methods in one switch... Too heavy, isn't it?)
So let A, a class which need a method comparison.
I suggest to create one class per method, which instance will be A's component.
For instance :
class A{
private:
//Some attributes
Comparator<A> comp_;
public:
//Some methods (including constructor)
bool operator()(const MyObject & first, const MoObject & second) const
{
return comp_.compare(first,second);
}
void setComparator(Comparator<A>& comp){
comp_ = comp;
}
}
//Forgot the syntax about template. So there is a template with one parameter
class Comparator{
public:
//Constructor
//To overwrite on subclasses
virtual bool compare(T& first, T& second) = 0;
}
With that configuration, you can easily add a method, without modifying A, just set the right comparator at any moment of program's execution.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want two classes. I have a static variable in class Class1 and I want to pass the value that it got to class Class2.
For Example :
//Class1.h
{
static int x;
int Method1();
}
//Class1.cpp
{
int Class1::x=0;
int Class1::Method1(){
x=2;
}
}
Now Class2
//Class2.cpp
{
Class1 cls;
cout<<cls.x<<endl;//it shows 0 value
}
I assume x is public:
#include "class1.h"
int xVal = Class1::x;
You need to declare the other class you want to access the variable from as "friend"
class Class1 {
friend class Class2;
// ...
}
Now you can access all variables from Class1 in Class2.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I had used an abstract base class for interface and derived class for implementation. See the code below.. Can it be associated with any standard design patterns in c++?
Class people
{
public:
virtual void setname(string name)=0;
virtual void SetAge(int Age)=0;
//etc consists of only pure virtual functions like above
};
Class Students: public people
{
void setname(string name)
{
//implementation of the function
}
void SetAge(int Age) { //implementation }
}
And i had defined many classes as above and objects are created in constructor of a Buildclass as:
Buildclass::Buildclass()
{
people *Obj = (people*) new Students();
interface *obj1 = (interface*) new implementation();
}
And i had provided getinstance functions for above to be used in another layer
void BuildClass::getPeopleinstance()
{
return Obj;
}
void BuildClass::getAnotherinstance()
{
return Obj1;
}
Can the above code be associated to any design pattern? Please let me know? I am unable to find out.
You appear to be using the Factory pattern, but it really doesn't matter. Focus on writing good code, not design patterns.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How should I get rid of the problem with object slicing in c++.
In my application if the derived class has some dynamically allocated pointer and derived class object is assigned to base class object, the behavior is memory corruption!
It depends on your design. You may have to change certain design criteria to get rid of it. One of the options is to have an overloaded operator = and copy constructor in your base class for particular derived class.
class Derived;
class Base
{
//...
private:
Base (const Derived&);
Base& operator = (const Derived&); // private and unimplemented
};
Now if you attempt to do something like following:
Derived d;
Base b;
b = d; // compiler error
it will result in compiler error.
you can't. you should solve the problem with the pointer. if you want to assign Obj2 to Obj1, override assign operator (operator=)
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I would like to get a vector/list of derived class pointers (one corresponding to each derived class from an abstract base class) given the base class name, for instance.
So to do exactly what you seems to want to do
#include <list>
class BaseObject { };
class Dinosaur : public BaseObject { };
class Hammer : public BaseObject { };
void
myFunction() {
std::list<BaseObject*> myList;
myList.push_back(new Dinosaur());
myList.push_back(new Hammer());
}
Note that the objects in the list won't free by themselves when the list will be destroyed. Either you have to do manually (Iterating over the list and calling delete), either look on something a bit complex if you are a C++ beginner, auto_ptr, and the magic world of the smart pointers ^^
You can't get a pointer to a class. A class is just a specification for an object. You only have something to point to once an object is created by instantiating the class.
If you have the following classes:
class FooBase {};
class DerivedFoo : public FooBase {};
class MoreDerivedFoo : public DerivedFoo {};
you can then create an object of type MoreDerivedFoo and point to it using a pointer of any of those types.
MoreDerivedFoo mdf = new MoreDerivedFoo();
FooBase* p1 = &mdf;
DerivedFoo* p2 = &mdf;
MoreDerivedFoo* p3 = &mdf;
Obviously each of these pointers will contain the same memory address.