Constructor in base and derived class - c++

Program works but I am not sure what is wrong with constructor since every time program runs it gets this error "warning: base class 'Alat' is uninitialized when used here to access 'Alat::ime' [-Wuninitialized]". I suppose it's something wrong how I called a constructor from base class but I am not sure what is problem. Really need help, tnx in advance.
#include <iostream>
#include <string>
using namespace std;
class Alat{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i);
string getIme();
void setSerBr(int sb);
int getSerBr();
void setCena(int c);
int getCena();
Alat();
Alat(string i, int sb, int c)
:ime(i),
serBr(sb),
cena(c)
{}
void info();
~Alat();
};
#include "Alat.h"
class Rucni : public Alat{
protected:
int minGodKor;
public:
Rucni():Alat(ime, serBr, cena) //I think here is problem, is it wrong called?
{}
int getminGodKor();
void setminGodKor(int min);
void info();
~Rucni();
};

Let the child default constructor call the default parent constructor, and create another child constructor with parameters to call the corresponding one of the parent:
#include <string>
using std::string;
class Alat
{
protected:
string ime;
int serBr;
int cena;
public:
void setIme(string i)
{
ime = i;
}
string getIme()
{
return ime;
}
void setSerBr(int sb)
{
serBr = sb;
}
int getSerBr()
{
return serBr;
}
void setCena(int c)
{
cena = c;
}
int getCena()
{
return cena;
}
Alat()
{
}
Alat(string i, int sb, int c) : ime(i), serBr(sb), cena(c)
{
}
~Alat()
{
}
};
class Rucni : public Alat
{
protected:
int minGodKor;
public:
Rucni() // implicit call of the parent default constructor
{
}
Rucni(string i, int sb, int c) : Alat(i, sb, c) // explicit call of the corresponding parent constructor
{
}
int getminGodKor()
{
return minGodKor;
}
void setminGodKor(int min)
{
minGodKor = min;
}
~Rucni()
{
}
};
int main()
{
Rucni r;
return 0;
}

Related

Segmentation fault on base class constructor

Why does it produce a segmentation fault when I create a Rectangle object? I'm thinking that there is something about my constructors that is incorrect but I don't have enough experience in C++ to identify what.
#include <string>
#include <map>
using namespace std;
class Shape {
private:
string name;
string property_name;
map<string,double> parameters;
public:
Shape(){
}
void set_name(string n){
name=n;
}
string set_property_name(string s){
property_name=s;
}
void set_parameter(string p, double n){
parameters[p]=n;
}
double get_parameter(string p) {
return parameters[p];
}
virtual double get_property() = 0;
};
class Shape2D: public Shape {
public:
Shape2D() {
set_property_name("area");
}
};
class Rectangle: public Shape2D {
public:
Rectangle() {
set_name("rectangle");
set_parameter("length",0);
set_parameter("base",0);
}
double get_property() {
return get_parameter("length") * get_parameter("base");
}
};
int main() {
Shape * user_shape;
user_shape=new Rectangle();
return 0;
}
Because your string set_property_name(string s) have no return and is undefined behavior

upcasting variable in derived class c++

How to change the type of a inherited variable in the derived class?
I have the following classes:
class Position;
class StonePosition;
class Position {
public:
Position() {}
};
class StonePosition : public Position {
int count;
public:
StonePosition(const int count) { this->count = count; }
int getCount() { return this->count; }
void setCount(int count) { this->count = count; }
friend ostream& operator<<(ostream&, StonePosition);
};
class Board {
protected:
Position* crrPos;
public:
Board() { }
Position* getCrrPos() { return crrPos; }
void setCrrPos(Position* pos) { crrPos=pos; }
};
class StoneBoard : public Board {
public:
StoneBoard(const int &count) { this->crrPos=new StonePosition(count); } //<----------------
StonePosition* getCrrPos() { return (StonePosition*)crrPos; }
void setCrrPos(StonePosition* pos) { crrPos=pos; }
};
Place in which the problem is marked by an arrow. I need to change the type of a variable from Position to StonePosition in the StoneBoard class. I found an option that can be used upcasting, but it works only within a single method, and I need to change the variable for the entire class.
The problem was solved, look at my answer.
The variable "crrPos" is not of type Position it is of type pointer to Position and this is significant because a pointer to Position can point to a Position or a class derived from Position without losing anything.
If you design your classes well, and make use of virtual functions, you can usually avoid the need to upcast entirely.
#include <iostream>
class Base {
public:
virtual void foo() { std::cout << "Base::foo()\n"; }
virtual bool isDerived() const { return false; }
};
class Derived : public Base {
public:
void foo() override { std::cout << "Derived::foo()\n"; }
bool isDerived() const { return true; }
};
int main() {
Base* crrPos = new Derived;
crrPos->foo();
bool isDerived = crrPos->isDerived();
std::cout << isDerived << '\n';
delete crrPos;
}
Live demo: http://ideone.com/UKcBaA
The problem has been solved, I just use the projection ((StonePosition*)Position*):
#include <iostream>
using namespace std;
class Position;
class StonePosition;
class Position {
public:
Position() {}
};
class StonePosition : public Position {
int count;
public:
StonePosition(const int count) { this->count = count; }
int getCount() { return this->count; }
void setCount(int count) { this->count = count; }
friend ostream& operator<<(ostream&, StonePosition);
};
template <typename TPos> class TBoard {
protected:
TPos* crrPos;
public:
TBoard() { }
TPos* getCrrPos() { return crrPos; }
void setCrrPos(TPos* pos) { crrPos=pos; }
};
class Board {
protected:
Position* crrPos;
public:
Board() { }
Position* getCrrPos() { return crrPos; }
void setCrrPos(Position* pos) { crrPos=pos; }
};
class StoneBoard : public Board {
public:
StoneBoard(const int &count) { this->crrPos=new StonePosition(count); }
Position* getCrrPos() { return crrPos; }
void setCrrPos(Position* pos) { crrPos=pos; }
};
int main(){
StoneBoard s(7);
cout<<((StonePosition*)s.getCrrPos())->getCount();//<----right here
system("pause");
return 0;
}
And its working nice :)

c++ - declaration is incompatible with (method) - pointers

I recently started playing around with c++, but for I don't understand what this means:
I get this error:
declaration is incompatible with "void
student_system::addStudent(<error-type> z)" (declared at line 31)
and the same goes for removeStudent and editStudent
I copied over the "function()" from another stackoverflow post, which seems fine and added the header with no issues, but my own "student" methods don't seem to work, I do not understand why,
I also tried adding the pointers rather than the variable but that didn't work either ( and by pointer I mean " student *x ").
#include "database.h"
#include <vector>
int main()
{
return 0;
}
class student_system
{
private:
list<student> studList;
public:
student_system();
void addStudent(student x);
void removeStudent(student y);
void editStudent(student z);
void findPos();
void function(int a, int b, vector<int> *p);
};
student_system::student_system()
{
//constructor
}
void student_system::addStudent(student x) // <------------- LINE 31
{
studList.push_back(x);
}
void student_system::removeStudent(student y)
{
/*studList.rem*/
}
void student_system::editStudent(student z)
{
/*get{ return value; }
set{ }*/
}
void student_system::findPos()
{
}
void student_system::function(int a, int b, vector<int> *p)
{
}
class student
{
private:
string name, surname, ID;
int sid;
public :
student::student(int sid, string n, string s, string id);
};
student::student(int sid, string n, string s, string id)
{
(*this).sid = sid;
(*this).name = n;
(*this).surname = s;
(*this).ID = id;
}
Put this bit of code
class student
{
private:
string name, surname, ID;
int sid;
public :
student::student(int sid, string n, string s, string id);
};
Just after the
#include <vector>
So that student_system and its definitions know about it
Add class student; above your student_system class definition.
Edit
Using forward declarations we can only declare methods in which we can use incomplete type not define. So, my first answer was wrong, but the following code will work.
int main()
{
return 0;
}
// Forward Declaration
class student;
// ================================
// Class student_system
// ================================
class student_system
{
private:
list<student> studList;
public:
student_system();
void addStudent(student x);
void removeStudent(student y);
void editStudent(student z);
void findPos();
void function(int a, int b, vector<int> *p);
};
// ================================
// Class student
// ================================
class student
{
private:
string name, surname, ID;
int sid;
public :
student(int sid, string n, string s, string id);
};
// ================================
// Definition of methods
// ================================
student::student(int sid, string n, string s, string id)
{
(*this).sid = sid;
(*this).name = n;
(*this).surname = s;
(*this).ID = id;
}
student_system::student_system()
{
//constructor
}
void student_system::addStudent(student x)
{
studList.push_back(x);
}
void student_system::removeStudent(student y)
{
/*studList.rem*/
}
void student_system::editStudent(student z)
{
/*get{ return value; }
set{ }*/
}
void student_system::findPos()
{
}
void student_system::function(int a, int b, vector<int> *p)
{
}
1) Please simplify a problem down to its basest elements before posting.
2) Please don't post code that relies on non-provided includes or implicit "using" statements or the like
3) "student" should be declared before student_system. The fact that you're not getting more errors surprises me, but maybe you're doing something unseen in database.h

Adapter pattern in C++, with non-virtual adapted methods

I want to create an adapter class in C++, but the interface that I want to adapt to has several non-virtual methods. Can I still use the regular adapter pattern?
#include <iostream>
using namespace std;
class NewInterface{
public:
int methodA(){ cout << "A\n"; }
virtual int methodB(){ cout << "B\n"; }
};
class OldInterface{
public:
int methodC(){ cout << "C\n"; }
int methodD(){ cout << "D\n"; }
};
class Old2NewAdapter: public NewInterface {
public:
Old2NewAdapter( OldInterface* a ){ adaptee = a; }
int methodA(){ return adaptee->methodC(); }
int methodB(){ return adaptee->methodD(); }
private:
OldInterface* adaptee;
};
int main( int argc, char** argv )
{
NewInterface* NI = new Old2NewAdapter( new OldInterface() );
NI->methodA();
NI->methodB();
return 0;
}
If I have this setup, the output will be "A D" instead of "C D" as it should.
So how can I adapt OldInterface to NewInterface, without rewriting NewInterface so that all methods are virtual?
Can you introduce another class? If you can then you can replace functions that use a NewInterface with an even NewerInterface:
class NewerInterface
{
public:
int methodA()
{
// preconditions
int const result = doMethodA();
// postconditions
return result;
}
int methodB()
{
// preconditions
int const result = doMethodB();
// postconditions
return result;
}
private:
virtual int doMethodA() = 0;
virtual int doMethodB() = 0;
};
class Old2NewerInterface : public NewerInterface
{
public:
explicit Old2NewerInterface(OldInterface& x) : old_(&x)
private:
virtual int doMethodA() { return old_->methodC(); }
virtual int doMethodB() { return old_->methodD(); }
private:
OldInterface* old_;
};
class New2NewerInterface : public NewerInterface
{
public:
explicit New2NewerInterface(NewInterface& x) : new_(&x)
private:
virtual int doMethodA() { return new_->methodA(); }
virtual int doMethodB() { return new_->methodB(); }
private:
NewInterface* new_;
};

Is there a way to forbid casting to subclass that is non-const in C++?

Here is a complete example.
I want to forbid using A::set from objects casted from B to A by allowing only casting
B to const A.
How to do it?
(I can't use virtual functions)
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { i_ = i; }
protected:
int i_;
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
void set(int i) { A::set(i); copy_i_ = i; }
protected:
int copy_i_;
};
void test2() {
A a;
a.set(3); // ok here
cout << a.get() << endl;
B b;
b.set(5);
A& aa = b;
assert(b.ok());
aa.set(3); // not ok here
assert(b.ok()); // fail-here
}
int main() {
test2();
return 0;
}
You could make the inheritance private and provide a member function in B to use instead of casting.
const A& B::convert_to_A() const { return *this; }
Why casting? Making void A::set(int i) protected will work in your case.
There is no need for forbidding non-const casts. You can solve your problem by using the template method design pattern.
#include "stdafx.h"
#include <iostream>
#include <cassert>
using namespace std;
class A {
public:
int get() const { return i_; }
void set(int i) { assert(i_ = i); copy_i();}
protected:
int i_;
virtual void copy_i(){};
};
class B : public A {
public:
int ok() const { return A::get() == copy_i_; }
protected:
int copy_i_;
void copy_i(){copy_i_ = i_; }
};
void test2() {
B b;
b.set(5);
A& a = b;
assert(b.ok());
a.set(3);
assert(b.ok()); // success!
}
int main() {
test2();
return 0;
}