clion class variable not initialized why appear z =16 - c++

my english not good ,sorry.
clion class variable not initialized why appear z =16
and vs2019 situation not same
#include <iostream>
using namespace std;
class bas {
public:
int showget();
private:
int x, z, y;
};
int main() {
bas B;
B.showget();
return 0;
}
int bas::showget() {
cout << x << " " << " " << z << " " << y << "\n";
int *t;
t = &x;
cout << t<<"\n";
t = &y;
cout << t<< "\n";
t = &z;
cout << t;
return 0;
}
}
}
enter image description here

Related

I have a problem with my c++ code. Error is C2280

I have an error in my code, I want to display the sume of 2 objects with pointers in a class. Please help me to fix it, maybe is due to the pointers. Can you see what's wrong?
This is the error:
<source>(79): error C2280: 'Pair &Pair::operator =(const Pair &)': attempting to reference a deleted function
<source>(60): note: compiler has generated 'Pair::operator =' here
<source>(60): note: 'Pair &Pair::operator =(const Pair &)': function was implicitly deleted because 'Pair' has a user-defined move constructor
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Pair {
int *x, *y;
public:
Pair() {
x = new int(sizeof(x));
y = new int(sizeof(y));
*x = 0;
*y = 0;
}
Pair(int a, int b) {
x = new int(sizeof(x));
y = new int(sizeof(y));
*x = a;
*y = b;
}
Pair(Pair& ob) {
x = new int(sizeof(ob.x));
y = new int(sizeof(ob.y));
*x = *(ob.x);
*y = *(ob.y);
}
Pair(Pair&& ob) {
x = new int(sizeof(ob.x));
y = new int(sizeof(ob.y));
*x = *(ob.x);
*y = *(ob.y);
}
Pair(int a):Pair(a, 0) {
}
void setX(int X) {
*x = X;
}
void setY(int Y) {
*y = Y;
}
int* getX() {
return x;
}
int* getY() {
return y;
}
~Pair() {
delete[]x;
delete[]y;
}
Pair sume(Pair ob1){
Pair ob2;
*(ob2.x) = *(ob1.x) + (*x);
*(ob2.y) = *(ob1.y) + (*y);
return ob2;
}
double media() {
return (double(*x) + double(*y)) / 2;
}
};
int main() {
Pair ob1, ob2(5), ob3(4, 3);
ob1.setX(6);
ob1.setY(7);
cout << "X= " << *(ob1.getX())<<endl;
cout << "Y= " << *(ob1.getY())<<endl;
cout << "Media este: " << ob1.media();
cout << "\nX= " << *(ob2.getX()) << endl;
cout << "Y= " << *ob2.getY() << endl;
cout << "Media este: " << ob2.media();
cout << "\nX= " << *(ob3.getX()) << endl;
cout << "Y= " << *(ob3.getY()) << endl;
cout << "Media este: " << ob3.media();
Pair ob4,ob5,ob6;
ob4 = ob1.sume(ob2);//here the compiler shows the error
cout <<"\nX= "<< *(ob4.getX())<<endl;
cout << "Y= " << *(ob4.getY())<<endl;
}

Clarification about function calls in cout needed

i'm doing an online c++ learning course with quiz. The last output line of this snippet is to be determined (comments added by me). Correct answer: 10. My question: why 10 and not 11?
Calling a(b) swaps the two variables, so why is the last a.a.b 0 and not 1? / Why does the a.b() in cout not affect the a.a.b?
#include <iostream>
using namespace std;
class classA {
public:
classA() { st.f = st.p = 1; }
struct { int f, p; } st;
int bfunc(void);
};
int classA::bfunc(void) { int x = st.f; st.f = st.p; st.p = x; return x; };
int main()
{
classA a;
a.st.f = 0;
cout << a.st.f << a.st.p << endl; //01
a.bfunc();
cout << a.st.f << a.st.p << endl; //10
a.bfunc();
cout << a.st.f << a.st.p << endl; //01
a.bfunc();
cout << a.st.f << a.st.p << endl; //10
cout << a.bfunc() << a.st.p << endl; //10
return 0;
}

How to use c++ structure array like flexible?

What should I do if I want to use the structure array flexibly?
I made my code like below, and tried Triangle and Rectangular..
Triangle was success but when i tried Rectangular I got error messages.
struct C2D {
double x, y;
};
class Polygon {
int point;
std::vector<C2D> arr;
public:
Polygon(int point_, C2D arr_[]) : arr(point_) {
point = point_;
memcpy(arr.data(), arr_, sizeof(C2D) * point);
};
void print() const {
for (int i = 0; i < point; i++) {
cout << arr[i].x << " " << arr[i].y << endl;
}
};
};
int main() {
int point;
C2D c2d[3];
cout << "point : ";
cin >> point;
cout << endl;
vector<C2D>c2d(point);
for (int i = 0; i < point; i++) {
cout << i + 1 << "x : ";
cin >> c2d[i].x;
cout << i + 1 << "y : ";
cin >> c2d[i].y;
cout << endl;
}
cout << endl;
Polygon p(point, c2d);
p.print();
return 0;
}
The problem is that you use the same name for
C2D c2d[3];
and
vector<C2D>c2d(point);
However, your code has several other issues such as using C-arrays in C++. Consider using std::vector, e.g., like this:
#include <iostream>
#include <vector>
struct C2D {
double x, y;
};
class Polygon {
std::vector<C2D> coordinates;
public:
explicit Polygon(const std::vector<C2D> &coords) : coordinates(coords) {}
void print() const {
for (const auto &c : coordinates) {
std::cout << c.x << " " << c.y << "\n";
}
}
};
int main() {
int point;
std::cout << "point : ";
std::cin >> point;
std::cout << "\n";
std::vector<C2D> coordinates(point);
for (int i = 0; i < point; i++) {
std::cout << i + 1 << "x : ";
std::cin >> coordinates[i].x;
std::cout << i + 1 << "y : ";
std::cin >> coordinates[i].y;
std::cout << "\n";
}
std::cout << "\n";
Polygon p(coordinates);
p.print();
}
As per your code, you declared variable name c2d one is C2D array type and the other is a vector of C2D what is the purpose of two variable names are the same you declared. My suggestion is to allocate c2d variable based on point count and pass to the polygon. As per my understanding change your polygon constructor with parameter array or vector and passing the corresponding argument to the constructor.

Modifying "Const Char Pointers" in C++

I am doing a program to test swapping couple of things by reference.
I managed to get the first two functions in my code to work but can't get to change the char * in the third function.
I think the problem is that it's a constant and only valid to read-only
that's what the error is telling me but How to be able to work with it in this way?
Here is the code:
#include <iostream>
using namespace std;
void swapping(int &x, int &y)
{
int temp =x;
x=y;
y=temp;
}
void swapping(float &x, float &y)
{
float temp=x;
x=y;
y=temp;
}
void swapping(const char *&x,const char *&y)
{
int help = *x;
(*x)=(*y);
(*y)=help;
} // swap char pointers
int main(void) {
int a = 7, b = 15;
float x = 3.5, y = 9.2;
const char *str1 = "One";
const char *str2 = "Two";
cout << "a=" << a << ", b=" << b << endl;
cout << "x=" << x << ", y=" << y << endl;
cout << "str1=" << str1 << ", str2=" << str2 << endl;
swapping(a, b);
swapping(x, y);
swapping(str1, str2);
cout << "\n";
cout << "a=" << a << ", b=" << b << endl;
cout << "x=" << x << ", y=" << y << endl;
cout << "str1=" << str1 << ", str2=" << str2 << endl;
return 0;
}
As suggested in the comments:
void swapping(const char*& x, const char*& y)
{
auto t = x;
x = y;
y = t;
}
Now you should consider to use a template:
template<typename Type>
void swapping(Type& a, Type& b)
{
auto t = a;
a = b;
b = t;
}

Accessing one namespace data member into another

How do I access the y (which is inside namespace n2), from n1 namespace:
The test code is below:
#include<iostream>
using namespace std;
namespace n1
{
int x = 20;
int m = ::n2::y;
void printx()
{
cout << "n1::x is " << x << endl;
cout << "n2::y is " << m << endl;
}
}
namespace n2
{
int y = 10;
}
int main()
{
cout << n1::x << endl;
n1::printx();
cout << n2::y << endl;
return 0;
}
I am getting below error:
test.cpp:7:15: error: ‘::n2’ has not been declared
int m = ::n2::y;
Just change the order, so that n2 is resolvable inside n1 :
#include<iostream>
using namespace std;
namespace n2
{
int y = 10;
}
namespace n1
{
int x = 20;
int m = n2::y;
void printx()
{
cout << "n1::x is " << x << endl;
cout << "n2::y is " << m << endl;
}
}
int main()
{
cout << n1::x << endl;
n1::printx();
cout << n2::y << endl;
return 0;
}