Accessing one namespace data member into another - c++

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;
}

Related

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;
}

clion class variable not initialized why appear z =16

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

Transform c++ class private variables to public

I am a first year student of programming, and I need some help.
I have code with public class but I need to change public to private. And it doesn't work for me. Maybe somebody can help me with some suggestions? Here's my working code with public objects, but I need to private. How can I do that?
This is my class's files:
Klientas.h:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
class Klientas
{
public:
string vardas;
double lesos;
};
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
Klipas.h :
class Klipas
{
public:
string produktas;
string grupe;
double trukme;
double krastine1;
double krastine2;
double plotas;
double klipoSekKaina;
double klipoKaina;
};
My code:
Lab_1.cpp
#include "Klipas.h"
#include "Klientas.h"
using namespace std;
//---------------------------------------------
int main() {
setlocale(LC_ALL, "Lithuanian");
Klipas K[100];
int na;
Klientas kl;
// Iš failo fv įveda duomenis į objektų masyvą K(kiek).
ĮvestiDuomenis("Duomenys.txt", K, na);
SpausdintiDuomenis("Rezultatai.txt", K, na);
}
void ĮvestiDuomenis(string fv, Klipas K[], int & kiek) {
ifstream fd ("Duomenys.txt");
fd >> kiek;
fd.ignore();
for (int i = 0; i < kiek; i++) {
getline(fd, K[i].produktas, ','); fd >> ws;
fd >> K[i].grupe;
fd >> K[i].trukme;
fd >> K[i].krastine1;
fd >> K[i].krastine2;
fd >> K[i].klipoSekKaina;
fd.ignore();
}
fd.close();
// cout << "Programa baigė darbą\n";
}
// Objektų masyvo K(kiek) reikšmes spausdina lentele į failą fv
void SpausdintiDuomenis(string fv, Klipas K[], int kiek) {
ofstream fr("Rezultatai.txt", ios::app);
fr.setf(ios::fixed); fr.setf(ios::left);
cout << "Klipų skaičius: " << kiek << endl;
cout << "Klipų sąrašas:\n";
cout << "----------------------------------------------------\n";
cout << "| Produktas | Grupė | Klipo trukmė(s) | 1 Kraštinė(cm) | 2 Kraštinė(cm) | Klipo sekundės kaina(Eur/s) | \n";
cout << "----------------------------------------------------\n";
for (int i = 0; i < kiek; i++) {
cout << "| " << setw(10) << K[i].produktas << "| " << K[i].grupe
<< setprecision(0) << setw(10) << "| " << K[i].trukme << "| " << setw(15) << K[i].krastine1 << "| " << setw(10) << K[i].krastine2 << "| " << setw(10) << K[i].klipoSekKaina << endl;
}
{
cout << "-------------------------------\n";
cout << "Klipo Kava plotas:" << K[0].krastine1*K[0].krastine2 << " " << "cm2" << endl;
cout << "Klipo Obuolys plotas:" << K[1].krastine1*K[1].krastine2 << " " << "cm2" << endl;
cout << "Klipo Sultys plotas:" << K[2].krastine1*K[2].krastine2 << " " << "cm2" << endl;
cout << "-------------------------------\n";
}
string ilg_trukme; // randame kuris klipas yra ilgiausias
if (K[0].trukme > K[1].trukme) {
ilg_trukme = K[0].produktas;
} else if (K[1].trukme > K[2].trukme) {
ilg_trukme = K[1].produktas;
} else {
ilg_trukme = K[2].produktas;
}
cout << "Ilgiausias klipas: " << ilg_trukme << endl;
cout << "-------------------------------\n";
{
K[0].klipoKaina = K[0].trukme * K[0].klipoSekKaina;
K[1].klipoKaina = K[1].trukme * K[1].klipoSekKaina;
K[2].klipoKaina = K[2].trukme * K[2].klipoSekKaina;
cout << "Klipo Kava Kaina:" << K[0].klipoKaina << " " << "Eur." << endl;
cout << "Klipo Obuolys Kaina:" << K[1].klipoKaina << " " << "Eur." << endl;
cout << "Klipo Sultys Kaina:" << K[2].klipoKaina << " " << "Eur." << endl;
cout << "-------------------------------\n";
}
{
string brangiausias_klipas; //randame kuris klipas brangiausias
double br_kl;
Klientas kl;
if (K[0].klipoKaina > K[1].klipoKaina && K[0].klipoKaina > K[2].klipoKaina ) {
brangiausias_klipas = K[0].produktas;
br_kl = K[0].klipoKaina;
} else if (K[1].klipoKaina > K[2].klipoKaina) {
brangiausias_klipas = K[1].produktas;
br_kl = K[1].klipoKaina;
} else {
brangiausias_klipas = K[2].produktas;
br_kl = K[2].klipoKaina;
}
cout << "Brangiausias klipas yra: " << brangiausias_klipas << endl;
cout << "-------------------------------\n";
cout << "Kiek jūs turite pinigų? " << endl; //kliento turimos pajamos
cin >> kl.lesos ;
cout << "-------------------------------\n";
//Randame kuriuos klipus klientas glaėtų įsigyti su savo pajamom
{
if(kl.lesos < K[0].klipoKaina && kl.lesos < K[1].klipoKaina && kl.lesos < K[2].klipoKaina) {
cout << "Jūs negalite nusipikrti nei vieno klipo " << endl;
} else {
if(kl.lesos >= K[0].klipoKaina) {
cout << "Jūs galite nusipirkti klipą " << K[0].produktas << endl;
}
if (kl.lesos >= K[1].klipoKaina) {
cout << "Jūs galite nusipirkti klipą " << K[1].produktas << endl;
}
if (kl.lesos >= K[2].klipoKaina) {
cout << "Jūs galite nusipirkti klipą " << K[2].produktas << endl;
}
}
}
}
}
Your teacher probably wants you to use getters.
In:
class Klipas
{
public:
string produktas;
string grupe;
double trukme;
double krastine1;
double krastine2;
double plotas;
double klipoSekKaina;
double klipoKaina;
};
You want to have access to all these members, but prevent external changes.
So you can change your code to:
class Klipas
{
public:
string GetProduktas() {return produktas;}
string Getgrupe() {return grupe;}
double Gettrukme() {return trukme;}
double Getkrastine1() {return krastine1;}
double Getkrastine2() {return krastine2;}
double Getplotas() {return plotas;}
double GetklipoSekKaina(){return klipoSekKaina;}
double GetklipoKaina() {return klipoKaina;}
private:
string produktas;
string grupe;
double trukme;
double krastine1;
double krastine2;
double plotas;
double klipoSekKaina;
double klipoKaina;
};
and use those getters instead of the objects themselves in your function:
fd >> K[i].Getgrupe();
fd >> K[i].Gettrukme();
fd >> K[i].Getkrastine1();
fd >> K[i].Getkrastine2();
fd >> K[i].GetklipoKaina();
As for setters go, you can either set your values in the constructor, or implement the same way:
public:
void SetProduktas(string prdkt) {produktas = prdkt;}
You can rewrite your class variables to be private and then use getters and setters to modify them.
class C {
private:
int id;
public:
int get_id() { return this->id; }
void set_id(int newId) { this->id = newId; }
};
Or you can make private class inside of another class
class A {
private:
class D {};
};

Code works in CodeBlocks, but gives errors when compiling in linux

main.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include "cootie.h"
using namespace std;
int main()
{
cout << "Time to create a Cootie!" << endl;
cootie c;
c.setName(name);
cout << "Add body parts." << endl;
cout << "1) Body" << endl << "2) Head" << endl << "3) Legs" << endl << "4) Wings" << endl << "5) Antennas" << endl << "6) Eyes" << endl;
cout << "Input 7 to print.";
while (roll != 7)
{
cin >> roll;
if (roll == 1)
{
c.setBody(numBody);
}
if (roll == 2)
{
c.setHead(numHead);
}
if (roll == 3)
{
c.setLeg(numLeg);
}
if (roll == 4)
{
c.setWing(numWing);
}
if (roll == 5)
{
c.setAntenna(numAntenna);
}
if (roll == 6)
{
c.setEye(numEye);
}
if (roll == 7)
{
c.print();
}
}
}
cootie.h
#ifndef COOTIE_H
#define COOTIE_H
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class cootie
{
public:
cootie();
int numLeg, numHead, numEye, numWing, numBody, numAntenna, roll;
string name = "Undefined";
int setName(string& name);
int setLeg(int& numLeg);
int setHead(int& numHead);
int setEye(int& numEye);
int setWing(int& numWing);
int setBody(int& numBody);
int setAntenna(int& numAntenna);
void print(string name, int numLeg, int numHead, int numEye, int numWing, int numBody, int numAntenna);
};
#endif // COOTIE_H
cootie.cpp
#include "cootie.h"
cootie::cootie()
{
numLeg;
numHead;
numEye;
numWing;
numBody;
numAntenna;
roll;
name = "Undefined";
}
int cootie::setName(string& name)
{
cout << "Name your Cootie!" << endl;
getline(cin, name);
}
int cootie::setBody(int& numBody)
{
numBody++;
}
int cootie::setHead(int& numHead)
{
numHead++;
}
int cootie::setWing(int& numWing)
{
numWing++;
}
int cootie::setLeg(int& numLeg)
{
numLeg++;
}
int cootie::setEye(int& numEye)
{
numEye++;
}
int cootie::setAntenna(int& numAntenna)
{
numAntenna++;
}
void cootie::print(string name, int numLeg, int numHead, int numEye, int numWing, int numBody, int numAntenna)
{
cout << "Cootie called " << name << endl;
cout << numLeg << " Leg(s)" << endl;
cout << numHead << " Head(s)" << endl;
cout << numEye << " Eye(s)" << endl;
cout << numWing << " Wings(s)" << endl;
cout << numBody << " Body(s)" << endl;
cout << numAntenna << " Antenna(s)" << endl;
}
I'm attempting to compile these files in linux, but i can't get the cootie.cpp to compile into source code. I tried using "g++ -c cootie.cpp" but i ended up getting errors.
cootie.h:11:22: error: ISO C++ forbids initialization of member 'numLeg'
cootie.h:11:22: error: making 'numLeg' static
cootie.h:11:22: error: ISO C++ forbids in-class initialization of non-const static member 'numLeg'
I tried compiling with the std=c++11 flag, but i got cc1plus: error: unrecognized command line option "-std=c++11" as an error, so i tried std=c++0x but that gave me the same errors i had initially.
Modified the code, but now i receive this error when i compile with the std=c++0x flag
/usr/lib64/gcc/x86_64-slackware-linux/4.5.2/../../../../lib64/crt1.o: In function _start':
/glibc-tmp-6341ca4bac93b935314b1241c89966aa/glibc-2.13/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference tomain'
collect2: ld returned 1 exit status
I'm pretty new to linux, so i'm not really sure whats causing the issue.
You can't initialize instance variables (e.g. numLeg) in the class definition in < C++11x. You have to initialize them in the constructor, e.g. like this:
cootie::cootie()
{
numLeg = 0;
numHead = 0;
numEye = 0;
numWing = 0;
numBody = 0;
numAntenna = 0;
roll = 0;
}
Just remove the = 0 in your header file before every instance variable.
This line is your problem:
int numLeg = 0, numHead = 0, numEye = 0, numWing = 0, numBody = 0, numAntenna = 0, roll = 0;
You need to drop the = 0's, end intitialize them in the constructor. I.o.w:
class cootie
{
public:
int numLeg, numHead, numEye, numWing, numBody, numAntenna, roll;
cootie::cootie()
: numLeg(0)
, numHead(0)
, ...
{
}
};

C++ how to overload method using pass by value and pass by reference

How do I make the following overload work
#include <iostream>
using namespace std;
int subtractFive (int a)
{
a = a - 5;
return a;
}
int subtractFive (int &a)
{
a = a -5;
return a -5;
}
int main()
{
int A = 10;
cout << "Answer: " << subtractFive(*A) << endl;
cout << "A Value "<< A << endl;
cout << "Answer: " << subtractFive(A) << endl;
cout << "A Value "<< A << endl;
return 0;
}
Tried but doesnt compile
#include <iostream>
using namespace std;
int subtractFive (int a)
{
a = a - 5;
return a;
}
void subtractFive (int* a)
{
*a = *a -5;
}
int main()
{
int A = 10;
cout << "Answer: " << subtractFive(A) << endl;
cout << "A Value "<< A << endl;
subtractFive(A);
cout << "A Value "<< A << endl;
return 0;
}
You might try specifying an overload that takes an address as an argument:
int subtractFive (int *a)
{
*a = *a -5;
return *a -5;
}
Declare one function as pass by address the other by value or reference:
void subtractByFive(int * p_value)
{
if (p_value != NULL)
{
*p_value -= 5;
}
return;
}
A value and a reference have the same type so you can't overload on it. If you want two functions one of which modifies its parameter and one that returns the new value then you either have to give them different names or different types (e.g. make the latter function use a pointer type).