constructor

can someone explain to me this line of code?
it is a constructor in a class but i dont get it

1
2
3
  LNot::LNot(Formula * right):BinaryOperation(new Variable("",uu), right)
{
}


thank you
:BinaryOperation(new Variable("",uu), right)

Initialization list -- in this case I assume it calls the constructor of the base class BinaryOperation, from which the class LNot is derived.

Does LNot look a bit like this?
1
2
3
4
class LNot: public BinaryOperation
{
    // ...
};

It is not necessary that BinaryOperation is a base class of LNot. It can be for example a data member of the LNot.
no actually BinaryOperations is a base class. but why did they use new variable ("",uu), cant i just write :BinaryOperations right(r)?
thank you a lot :)
Why are you asking us?! It is your code. Look through your code.
Last edited on
closed account (3qX21hU5)
What vlad means is we can't be sure why they used that because we have no idea what the class BinaryOperation looks like. We would need to see that before we can answer that question
no actually BinaryOperations is a base class. but why did they use new variable ("",uu), cant i just write :BinaryOperations right(r)?

Because you don't declare variables / data members in an initialization list. An initialization list initializes existing data members or calls the constructor of the base class. Which is what happens in your case, see the constructor of BinaryOperation.

Edit: also, I recommend reading about inheritance:
http://cplusplus.com/doc/tutorial/inheritance/#inheritance
Last edited on
actually vlad this is not my code, if it was i wouldnt have asked someone to explain what is happening. it is my professors code.

here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef FORMULA_H
#define FORMULA_H
#include <iostream>

using namespace std;
enum TriValue{uu,tt,ff};
class Formula
{
public:
	TriValue formulaValue;
	Formula();
	Formula(TriValue v);

	void setValue(TriValue v);
	TriValue getFormulaValue();
	virtual TriValue evaluate();
	virtual ostream & PrintOstreamOperator(ostream &os);
};
ostream & operator <<(ostream &os, Formula *f);
#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "Formula.h"
Formula::Formula(){
	formulaValue=uu;

}

Formula::Formula(TriValue v)
{
	formulaValue= v;
}

void Formula::setValue(TriValue v)
{
	formulaValue= v;
}
TriValue Formula::getFormulaValue()
{
	return formulaValue;
}

TriValue Formula::evaluate() //this is a base virtual function. A formula can be a subtype (of child class) variable or and, or, nor. 
							 //according to its type, the corresponding function will be called.
{
	return formulaValue;
}

ostream & Formula::PrintOstreamOperator(ostream &os){
	return os;
}
ostream & operator <<(ostream &os, Formula *f)
{
	return f->PrintOstreamOperator(os);
	//return os;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef VARIABLE_H
#define VARIABLE_H
#include <iostream>
using namespace std;
#include "Formula.h"

//enum TriValue{uu,tt,ff};
class Variable: public Formula
{
public:
	char name[8];
	Variable();
	Variable(char n[8],TriValue v);

	void setName(const char * aName);
	virtual ostream&  PrintOstreamOperator(ostream &os);
	virtual TriValue evaluate();
		
};

ostream & operator <<(ostream & os, Variable *var);

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Variable.h"
#include <cstring>

Variable::Variable()
{	
	name[0]='\0';
	formulaValue=uu;
}

Variable::Variable(char n[8],TriValue v): Formula(v)
{
	strncpy(name,n,8);
}

void Variable::setName(const char * aName)
{
	strncpy_s(name,aName,8);
	/*
		int i=0; //iterator
	
	while(i<size){
	
		name[i]=aName[i];
		i=i+1;

	}
	*/
}


TriValue Variable::evaluate()
{
	return formulaValue;
}


ostream & Variable::PrintOstreamOperator(ostream &os)
{
		os<<name;
		return os;
}



ostream & operator <<(ostream &os, Variable *var)
{	
	return var->PrintOstreamOperator(os);
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef BINARYOPERATION_H
#define BINARYOPERATION_H
#include "Formula.h"
#include "Variable.h"
#include <iostream>
using namespace std;
class BinaryOperation: public Formula
{
public:
	Formula* leftOperand;
	Formula* rightOperand;
	virtual ostream & printSeparator(ostream &os);
	virtual ostream & PrintOstreamOperator(ostream &os);
	virtual TriValue evaluate();
	BinaryOperation(Formula *l, Formula *r);
	BinaryOperation();

};

ostream & operator << (ostream & os, BinaryOperation * BinOp);

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "BinaryOperation.h"

BinaryOperation::BinaryOperation()
{
	leftOperand= NULL;
	rightOperand= NULL;
}

BinaryOperation::BinaryOperation(Formula *l, Formula *r):leftOperand(l), rightOperand(r)	
{
}

TriValue BinaryOperation::evaluate()
{
	return uu;// anything, since this won't effectively be called
}

ostream & BinaryOperation::printSeparator(ostream &os)
{
	return os;	
}

ostream & BinaryOperation::PrintOstreamOperator(ostream &os)
{
	os<<"("<<leftOperand;
	printSeparator(os);
	os<<rightOperand<<")";
	return os;
}

ostream & operator << (ostream & os, BinaryOperation * BinOp)
{
	
	return BinOp->PrintOstreamOperator(os);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef LNOT_H
#define LNOT_H
#include "Formula.h"
#include "BinaryOperation.h"

class LNot: public BinaryOperation
{
public:
	LNot();
	LNot(Formula * right);
	
	virtual ostream & printSeparator(ostream &os);
	virtual TriValue evaluate();
	

};
ostream & operator <<(ostream &os, LNot *l);

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
#include "LNot.h"
#include "BinaryOperation.h"
#include "Variable.h"


LNot::LNot(Formula * right):BinaryOperation(new Variable("",uu), right)
{
}

TriValue LNot::evaluate()
{
	TriValue RightOpValue= rightOperand->evaluate();

	if(RightOpValue==ff)	{return tt;}
	else	{return ff;}
	
}
ostream & LNot::printSeparator(ostream &os)
{
	os<<"!";
	return os;	
}

ostream & operator <<(ostream &os, LNot *l)
{
	l->PrintOstreamOperator(os);
	return os;
}



thank you :)
Topic archived. No new replies allowed.