Newly I've begun learning OOP.I've written simple program.But function int sum() has errors.Can you write decision of the problem.And explain errors.
class Int
{
private:
int num;
public
Int()
{
num = 0;
}
Int(int n)
{
num = n;
}
int getValue()
{
return num;
}
int sum(const Int& n)
{
return num+ n.getValue();
}
void main()
{
short a, b;
cout << "Enter 2 numbers:";
cin >> a >> b;
Int A(a), B(b), res;
cout << A.getValue() << " + " << B.getValue() << " = " << A.sum(B) <<"\n\n";
}
Below is my attempt. There were some formatting issues but the trickiest problem was that getValue() is a const function.
class Int {
private:
int num;
public:
Int() { num = 0; }
Int(int n) { num = n; }
// getValue is a const function as it does not alter object 'n'.
int getValue() const { return num; }
int sum(const Int& n) {
int ret = num + n.getValue();
return ret;
}
};
int main() {
int a, b;
std::cout << "Enter 2 numbers:";
std::cin >> a >> b;
Int A(a), B(b), res;
std::cout << A.getValue() << " + " << B.getValue() << " = " << A.sum(B) << "\n\n";
}
You cannot declare a function inside another function like that. You have main() inside sum().
Related
cube is a class that from what i know can be both constexpr and not and for some reason c.get() is not constexpr because the second cout prints 5 which mean it changed the value of c to 5 instead of c.get() always returning 1.
class cube
{
private:
int roll;
public:
constexpr cube(const int& r) :roll(r) {}
void set(int const& a) { roll = a; }
constexpr int get() const { return roll; }
constexpr void fun() const
{
(const_cast <cube*> (this))->roll = 5;
}
};
int main()
{
constexpr cube c(1);
std::cout << "Old roll number: " << c.get() << std::endl;
c.fun();
std::cout << "New roll number: " << c.get() << std::endl;
return 0;
}
EDIT:
in the comments some said that the fun() breaks it but here it's still not constexpr as you can see here gcc.godbolt.org/z/zoz7KEqqn
#include<iostream>
class cube
{
private:
int roll;
public:
constexpr cube(const int& r) :roll(r) {}
void set(int const& a) { roll = a; }
constexpr int get() const { return roll; }
};
int main()
{
constexpr cube c(1);
std::cout << "Old roll number: " << c.get() << std::endl;
std::cout << "New roll number: " << c.get() << std::endl;
return 0;
}
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 2 months ago.
For some reason the value of divide() still prints as an int even though I declared it as a double.
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
class Calculator{
private:
int a = 0;
int b = 0;
public:
Calculator(int n1, int n2) : a(n1), b(n2) {}
Calculator() = default;
int getN1() const{
return a;
}
int getN2() const{
return b;
}
int add() const{
return a + b;
}
int subtract() const{
return a - b;
}
int multiply() const{
return a * b;
}
double divide() const{
return a / b;
}
void showDetails() const{
cout << " " << endl;
cout << "Computed:" << endl;
cout << "Sum = " << add() << endl;
cout << "Difference = " << subtract() << endl;
cout << "Product = " << multiply() << endl;
cout << "Quotient = " << setprecision(2) << fixed << divide() << endl;
}
};
int main() {
int n1;
int n2;
cout << "Enter Num1: ";
cin >> n1;
cout << "Enter Num2: ";
cin >> n2;
Calculator x(n1, n2);
x.showDetails();
return 0;
}
It worked when I declared the initial values as double but I was tasked to assign the initial values as int.
a and b are declare as integers. Make it double. int upon int gives int value hence change any one or both of them to double.
My program receives width and breadth of rectangle
My output would be rectangle and specific point which can get from second line input. If number is odd , it will represent 'y' coordinate, else it will represent 'x' coordinate.
My problem is
pt.mark(pt.get_p(), pt.get_q());
this one.
In my method "mark",
buf << "+-" << setw(x() - 2) << "" << "-+" << endl;
x() represents " 0 " . So , my rectangle looks like
I don't know why this happen , because my approach to the private number of Class "Point" is not wrong using method x().
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
using namespace std;
class Point
{
private:
int _x, _y;
public:
Point(int x=0, int y=0): _x(x), _y(y) {}
int x() { return _x; }
int x(int n) {return _x = n;}
int y() { return _y; }
int y(int n) {return _y=n;}
};
class MovingPoint: public Point
{
private:
int p, q;
vector<int> nums;
public:
MovingPoint(int x = 0, int y = 0): Point(x, y) {}
MovingPoint(vector<int> a) : nums(a) {}
void mark(int x, int y);
void calculate();
int get_p() {return p;}
int get_q() {return q;}
};
ostream& operator <<(ostream& out, MovingPoint p)
{
return out << "(" << p.x() << ", " << p.y() << ")";
}
void MovingPoint::mark(int a, int b)
{
ostringstream buf;
vector<int> lengths;
vector<string> words;
int cnt;
char prev = buf.fill('-');
buf << "+-" << setw(x() - 2) << "" << "-+" << endl;
char pres = buf.fill(' ');
for (int i = 0; i < y() ; i++)
{
if (i == b - 1)
buf << "| " << setw(a) << "." << setw(x() - a) << " |" << endl;
else
buf << "| " << setw(x()) << " |" << endl;
}
char prev1 = buf.fill('-');
buf << "+-" << setw(x() - 2) << "" << "-+" << endl;
buf.fill(prev1);
cout << buf.str();
}
void MovingPoint::calculate()
{
p = 0;
q = 0;
for (int i = 0; i < nums.size() ; i++)
{
if (nums[i] % 2 == 0)
p++;
else
q++;
}
}
int main()
{
int x;
int y;
vector<int> nums;
cin >> x >> y;
MovingPoint pt(x, y);
while (cin >> x)
{
nums.push_back(x);
}
pt = MovingPoint(nums);
pt.calculate();
pt.mark(pt.get_p(), pt.get_q());
}
You might do, with some renaming:
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
struct Rectangle
{
unsigned int width;
unsigned int height;
};
struct Point
{
unsigned int x;
unsigned int y;
};
void draw(const Rectangle& rect, const Point& pt)
{
std::ostringstream buf;
buf.fill('-');
buf << "+-" << setw(x() - 2) << "" << "-+" << endl;
buf.fill(' ');
for (unsigned int i = 0; i < rect.height() ; i++)
{
if (i == pt.y - 1)
buf << "| " << setw(pt.x) << "." << setw(rect.width() - pt.x) << " |" << endl;
else
buf << "| " << setw(rect.width()) << " |" << endl;
}
char prev1 = buf.fill('-');
buf << "+-" << setw(rect.width() - 2) << "" << "-+" << endl;
buf.fill(prev1);
std::cout << buf.str();
}
Point calculate(const std::vector<int>& v)
{
Point p{0, 0};
for (auto e : v)
{
if (e % 2 == 0)
p.x++;
else
p.y++;
}
return p;
}
int main()
{
Rectangle r;
std::cin >> r.width >> r.height;
std::vector<int> nums;
int n;
while (std::cin >> n)
{
nums.push_back(n);
}
Point pt = calculate(nums);
draw(r, pt);
}
I am trying to write a class with a map to keep a registry with unique ID for later accessing the objects. All compiled fine till i wrote the for loop in main trying to access objects of the class and their data. I am at a loss what is exactly wrong. I declared all static but doesn't work. I have been trying for several hours now but couldn't solve it. I know the problem is in the map as that is new for me, but i can't seem to find the issue. Hope someone sees what is wrong in my code.
#include <iostream>
#include <map>
#include <cassert>
#include <string>
#include <algorithm>
using namespace std;
class vertegenwoordiger{
public:
vertegenwoordiger(int id, string x, int y): ID(id), name(x),aantalpc(y) {
addtoregistry(this);
cout << "Vertegenwoordiger " << x << " is aangemaakt met " << y << " aantal verkochte pc's " << endl;
gemiddeldeverkoop = (gemiddeldeverkoop + y) / id;
}
static map<int, vertegenwoordiger*>registryMap; // PROBLEM HERE I GUESS
static void addtoregistry(vertegenwoordiger* object){
registryMap[object->ID] = object;
}
static void removefromregistry(vertegenwoordiger* object){
registryMap.erase(object->ID);
}
static vertegenwoordiger* findbymap(int id){
return registryMap[id];
} // MAYBE THIS FUNCTION IS NOT CORRECT ASWELL????
void commissionfixed (vertegenwoordiger* obj){
commissievast = obj->aantalpc*winstperpc;
}
void commissionextra (vertegenwoordiger*obj){
if (obj->aantalpc>gemiddeldeverkoop){
commissieplus = (obj->aantalpc - gemiddeldeverkoop) * 37;
}
}
static const int winstperpc;
static int gemiddeldeverkoop;
const int ID;
protected:
string name;
int aantalpc;
int commissievast;
int commissieplus;
};
const int vertegenwoordiger::winstperpc = 150;
int vertegenwoordiger::gemiddeldeverkoop = 0;
int main()
{
for (int i=0; i<4; i++){
string naam;
int pc;
cout << "geef naam in :";
cin >> naam;
cout << "geef aantal pc op :";
cin >> pc;
vertegenwoordiger* test = new vertegenwoordiger (i+1,naam,pc);
cout << "volgende aub : " << endl;
}
for (int i=1; i<4 ; i++){
vertegenwoordiger* val = vertegenwoordiger::findbymap(i); // I GUESS THE PROBLEM IS RELATED TO THIS LINE
vertegenwoordiger::commissionfixed (val);
vertegenwoordiger::commissionextra (val);
}
return 0;
}
#include <iostream>
#include <map>
#include <cassert>
#include <string>
#include <algorithm>
using namespace std;
class vertegenwoordiger{
public:
vertegenwoordiger(int id, string x, int y): ID(id), name(x),aantalpc(y){
addtoregistry(this);
cout << "Vertegenwoordiger " << x << " is aangemaakt met " << y << " aantal verkochte pc's " << endl;
gemiddeldeverkoop = (gemiddeldeverkoop + y) / id; }
static map<int, vertegenwoordiger*>registryMap;
static void addtoregistry(vertegenwoordiger* object){ registryMap[object->ID] = object; }
static void removefromregistry(vertegenwoordiger* object){ registryMap.erase(object->ID); }
static vertegenwoordiger* findbymap(int id){ return registryMap[id]; }
void commissionfixed (){
commissievast = this->aantalpc*winstperpc; }
void commissionextra (){
if (this->aantalpc>gemiddeldeverkoop){ commissieplus = (this->aantalpc - gemiddeldeverkoop) * 37; } }
void readCommission(){
cout << "comission payment is " << this->commissievast << endl;
}
static const int winstperpc;
static int gemiddeldeverkoop;
const int ID;
string name;
protected:
int aantalpc;
int commissievast;
int commissieplus;
};
map<int, vertegenwoordiger*>vertegenwoordiger::registryMap;
const int vertegenwoordiger::winstperpc = 150; int vertegenwoordiger::gemiddeldeverkoop = 0;
int main() {
for (int i=0; i<2; i++){ string naam; int pc; cout << "geef naam in :";
cin >> naam;
cout << "geef aantal pc op :";
cin >> pc;
vertegenwoordiger* test = new vertegenwoordiger (i+1,naam,pc);
cout << "next please : " << endl; }
for (int i=1; i<3 ; i++){
vertegenwoordiger* val = vertegenwoordiger::findbymap(i);
val->commissionfixed ();
val->commissionextra ();
val->readCommission ();
}
return 0; }
This is the photo of the model I have to resolve:
I have this class:
#include<iostream>
#include<fstream>
using namespace std;
class Word
{
protected:
char *value;
char type[20];
int noChars;
static int noWords;
public:
Word(char *value, char *type)
{
this->noChars = 0;
this->value = new char[strlen(value) + 1];
strcpy(this->value, value);
strcpy(this->type, type);
Word::noWords++;
}
Word()
{
this->noChars = NULL;
this->value = NULL;
strcpy(this->type,"Nedeterminat");
}
void operator=(Word &x)
{
this->noChars = x.noChars;
strcpy(this->type, x.type);
this->value = new char[strlen(x.value) + 1];
strcpy(this->value, x.value);
}
Word(const Word& x){
this->noChars = x.noChars;
strcpy(this->type, x.type);
this->value = new char[strlen(x.value) + 1];
strcpy(this->value, x.value);
}
char* getValue()
{
return this->value;
}
void setType(char* x)
{
if (x == NULL)
{
throw new exception("Tip gresit!");
}
else
{
strcpy(this->type, x);
}
}
char &operator[](int i)
{
if (i >= 0 && i <= (strlen(this->value) - 1))
{
return this->value[i];
}
else
cout << endl << "Eroare indice: " << i;
}
static int getNoWords()
{
return Word::noWords;
}
operator int()
{
return this->noChars;
}
friend ostream& operator<<(ostream&, Word&);
friend istream& operator>>(istream&, Word&);
};
ostream& operator<<(ostream& consola, Word& x)
{
consola << "Value: " << x.getValue() << endl;
consola << "Type: " << x.type << endl;
consola << "NoChars: " << x.noChars << endl;
return consola;
}
istream& operator>>(istream& consola, Word& x){
cout << "Value: "; consola >> x.value;
cout << "Type: "; consola >> x.type;
cout << "NoChars: "; consola >> x.noChars;
return consola;
}
int Word::noWords = 0;
class Dictionary{
private:
char *language;
int noWords;
bool isOnline;
Word v[100];
public:
Dictionary(char *language, Word w, int noWords, bool isOnline)
{
this->language = new char[strlen(language) + 1];
strcpy(this->language, language);
for (int i = 0; i < 100; i++)
{
this->v[i] = w;
}
this->noWords = noWords;
this->isOnline = isOnline;
}
};
int main()
{
//1
Word w1("exam", "noun");
/*Word w2;*/
Word w3 = w1;
cout << w3;
//2
cout << endl << "Word value: " << w3.getValue();
Word w2("to take", "noun");
w2.setType("verb");
//3
w3 = w2;
cout << endl << w3;
Word *pw = new Word("pointer", "noun");
delete pw;
//4
cin >> w3; cout << w3;
char character = w3[2];
cout << endl << character;
//5
double noChars = (int)w1;
cout << endl << noChars;
cout << endl << Word::getNoWords() << endl;
//6
Dictionary dictionary1("English", NULL, 0, false);
}
I have this main:
Dictionary dictionary1("English", NULL, 0, false);
How should I change the constructor to work? I receive a error :
Arrgument types are:(const char[8],int,int,bool);
And how should I write the default constructor?
NULL cannot be assigned to Word. Try Word() instead which will call the default constructor to Word. Consider changing that function parameter to const Word& - anonymous temporaries are allowed to bind to const references.
I'd prefer to see a std::string as the type for the member variable language; using a const std::string& as the function parameter. Then you will not have to worry about a subsequent delete call, and defining your own assignment operators and copy constructors. Currently, your class leaks memory.
You can not Assign null to the type Word. If you want to default it you should pass something like word()
Dictionary dictionary1("English", word(), 0, false);
EDIT:
The better approach,IMO, that will work for the same main() you have is like this:
class Dictionary{
private:
std::string language;
int noWords;
bool isOnline;
std::array<Word,100> v;
public:
Dictionary(std::string const& language, Word* w, int noWords, bool isOnline): language (language),isOnline(isOnline ),noWords(noWords)
{
for (int i = 0; i < 100; i++){
this->v[i] = (w==NULL)?word():*w;
}
}
};