I have a test next week for c++ and I'm preparing myself for it. I'm confused when I have 2 classes as shown below. I have to walk through the execution of the code, line by line, and I'm confused about the marked lines (x = ... and y = ... inside class two) - where does execution go from there?
#include <iostream>
using namespace std;
class one {
int n;
int m;
public:
one() { n = 5; m = 6; cout << "one one made\n"; }
one(int a, int b) {
n = a;
m = b;
cout << "made one one\n";
}
friend ostream &operator<<(ostream &, one);
};
ostream &operator<<(ostream &os, one a) {
return os << a.n << '/' << a.m << '=' <<
(a.n/a.m) << '\n';
}
class two {
one x;
one y;
public:
two() { cout << "one two made\n"; }
two(int a, int b, int c, int d) {
x = one(a, b); //here is my problem
y = one(c, d); //here is my problem
cout << "made one two\n";
}
friend ostream &operator<<(ostream &, two);
};
ostream &operator<<(ostream &os, two a) {
return os << a.x << a.y;
}
int main() {
two t1, t2(4, 2, 8, 3);
cout << t1 << t2;
one t3(5, 10), t4;
cout << t3 << t4;
return 0;
}
from the line x = one(a, b);
it jumps to line
one(int a, int b)
and executes the parameterized constructor of one
same for line y = one(c, d);
x = one(a, b); //here is my problem
y = one(c, d); //here is my problem
What this code does is that it calls the constructor of the class one and assigns the newly created instance of this class to the variables x and y.
The constructor of class one is in line 9.
Current approach works only if you have a default constructor in one class.
It is better to initialize members in constructor initialization list:
two(int a, int b, int c, int d)
: x(a,b), y(c,d)
{
cout << "made one two\n";
}
Related
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}
Here operator + is overloaded and it is accesing the private member of res class
Some more examples are
ex1 -
struct Edge {
int a,b,w;
};
bool operator<(const Edge& x, const Edge& y) { return x.w < y.w; }
ex2-
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int a,b,w;
bool operator<(const Edge& y) { return w < y.w; }
};
int main() {
int M = 4;
vector<Edge> v;
for (int i = 0; i < M; ++i) {
int a,b,w; cin >> a >> b >> w;
v.push_back({a,b,w});
}
sort(begin(v),end(v));
for (Edge e: v) cout << e.a << " " << e.b << " " << e.w << "\n";
}
ex1 and ex2 are from the usaco.guide and the first example was from the geeks for geeks
Can anyone explain how it works ?
How here private data members are accessed from the class instance. Can anyone explain how it works ?
First things first, in ex1 and ex2 you're using a struct and so by default every member is public. So any user of the class has access to its members.
Now, even if those data members were private, you've overloaded operator+ and operator< as member functions. And a member function has full access to any(private, public or protected) member of the corresponding class type.
#include <bits/stdc++.h>
using namespace std;
class point
{
public:
int x, y;
point(int a, int b)
{
x = a, y = b;
}
bool operator==(point &a)
{
if (a.x == x && a.y == y)
{
return 1;
}
else
{
return 0;
}
}
};
int main()
{
point a(5, 7), b(5, 7);
int t = a == b;
cout<<t; //it is working properly
cout<< (a==b); // it also
cout << a==b; //but it gives me compilation error
}
what is the reason...??
is there any fact of operator precedence...??
I don't know why it is giving compilation error...
as it is seen it looks fine
The << operator has higher precedence than the == operator. So this:
cout << a==b;
Is parsed as this:
(cout << a ) == b;
This causes an error because there is no overload of operator<< for cout that takes point as an argument.
The explicit parenthesis in your first example is the proper way to handle this.
I am trying to understand an output of a short program where operator overloading is used.
The output is 137, where the (2+v).print() outputs 13 and 7 is from v.print();
#include <iostream>
using namespace std;
class V
{
int x;
public:
V(int a = 7, int b = 3) { x = a + b; }
void print() { cout << x; }
V operator+(int n)
{
return x++ + ++n;
}
};
V operator+(int lop, V rop)
{
return rop + lop;
}
int main()
{
V v(1, 6);
(2 + v).print();
v.print();
return 0;
}
I understand the basic concept of the operator overloading and I get that V rop is just a copy of the V v(1,6), and it doesn't change the output of v.print(); where x stays 7, but I don't get why it outputs 13, I always get to 10.
The problem is when trying to return an object of type 'V' from this operator:
V operator+(int n)
{
return x++ + ++n;
}
What you're trying to return here in an 'int' so it should be cast to an object of type 'V', the way it's done (casting from a primitive type to custom class) is using constructors. The only constructor you're having is with 2 optional parameters which creates the problem that it tries to make an object of one parameter only, so it's sent as a = 10, b = 3 (default value) and then the output is 13.
I recommend using multiple constructors to solve the problem if you don't want to change the members of the class.
class V
{
int x;
public:
V() { x = 10; }
V(int a) { x = a; }
V(int a, int b) { x = a + b; }
void print() { cout << x; }
V operator+(int n)
{
return x++ + ++n;
}
};
By this way you can call a default constructor which sets x to 10 as you previously did, another constructor with 1 parameter to cast from 'int' to 'V', and your normal constructor that takes a and b.
In your code when arrive at return (x++ + ++n); compiler create an object V so your constructor will be call again. then these assignment will be occurred a=10 and b=3. So you gotta save a and b values in another members.
Try this :
#include <iostream>
using namespace std;
class V {
int x;
int a;
int b;
public:
V(int a=7, int b=3) { x = a + b; this->a = a; this->b = b; }
void print() { cout << x - this->b; }
V operator+(int n) {
return (x++ + ++n);
}
};
V operator+(int lop, V rop) {
return rop + lop;
}
int main()
{
V v(1,6);
(2 + v).print();
v.print();
return 0;
}
Your (2 + v).print(); output will be 10.
#include "iostream"
using namespace std;
class Algebra
{
private:
int a, b;
const int c;
const int d;
static int s;
public:
//default constructor
Algebra() : c(0), d(0)
{
s++;
a = b = 0;
cout << "Default Constructor" << endl;
}
//parameterized (overloaded) constructor
Algebra(int a, int b, int c1, int d1) : c(c1), d(d1)
{
s++;
setA(a);
setB(b);
cout << "Parameterized Constructor" << endl;
}
//copy (overloaded) constructor
Algebra(const Algebra &obj) : c(obj.c), d(obj.d)
{
s++;
this->a = obj.a;
this->b = obj.b;
cout << "Copy Constructor" << endl;
}
//Destructor
~Algebra()
{
s--;
cout << "Destructor Called" << endl;
}
//Setter for static member s
static void setS(int s)
{
Algebra::s = s;
}
//Getter for static member s
static int getS()
{
return s;
}
//Getter for constant data member c
int getC() const
{
return this->c;
}
//Setter for data member a
void setA(int a)
{
if(a < 0)
this->a = 0;
else
this->a = a;
}
//Setter for data member b
void setB(int b)
{
if(b < 0)
this->b = 0;
else
this->b = b;
}
};
int Algebra::s = 90;
int main()
{
Algebra obj1, obj2(1, 2, 3,4), obj3(obj1);
cout << "Size of object = " << sizeof(obj1) << endl;
return 0;
}
why the sizeof operator show size to be 16,where i have declared 5 data members of int type.it should add up all 5 data members and give the result as 20. i have also checked sizeof operator on static int variable separately which works fine.
The static member doesn't contribute to the size of a class instance, as you only need one copy for your entire program, not one per instance. Therefore, the size consists of 4 ints, which is 16 bytes on your platform.
I am a beginner at C++ and I am trying to make a program that uses 2 points, adds them together to a line. Then adds a line together with a point, and gets a polygon.
I am trying, unsuccessfully, to overload the operator << so that I can print out my line:
#include <iostream>
using namespace std;
class Line {
private:
OnePoint onevalue;
OnePoint twovalue;
public:
Line(OnePoint a, OnePoint b) {
onevalue = a;
twovalue = b;
}
ostream& operator<<(ostream& print, Line& linje){ // Error right here.
print << linje.onevalue << ',' << linje.twovalue; // Too many
return print; // parameters for this type of function
}
};
class OnePoint {
private:
double xvalue;
double yvalue;
public:
OnePoint(double x = 0.0, double y = 0.0) {
xvalue = x;
yvalue = y;
}
friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
return printh;
}
void Plus(OnePoint a) {
xvalue = xvalue + a.xvalue;
yvalue = yvalue + a.yvalue;
}
void Minus(OnePoint b) {
xvalue = xvalue + b.xvalue;
yvalue = yvalue + b.yvalue;
}
OnePoint Plustwo(OnePoint a) {
return (xvalue + a.xvalue, yvalue - a.yvalue);
}
void Change(double a, double b) {
xvalue += a;
yvalue += b;
}
void Print(OnePoint b) {
cout << xvalue << "," << yvalue << endl;
}
OnePoint operator+(OnePoint a) {
OnePoint temp;
temp.xvalue = xvalue + a.xvalue;
temp.yvalue = yvalue + a.yvalue;
return temp;
}
};
//--------------------------------------------------------------------
int main(){
OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0);
OnePoint d(1.0, 4.0);
OnePoint c;
c = a + b + d;
c.Print(c);
}
Edit:
I can now cout my OnePoint, but I cannot cout Line.
As a member function, the operator gets an extra hidden parameter for the this pointer.
You can make it a free function by declaring it a friend of the class:
class Line {
// other members
public:
friend ostream& operator<<(ostream& print, Line& linje){
print << linje.onevalue << ',' << linje.twovalue;
return print;
}
};