Why i is being called private?as i is static member ,then it must not an error in line no.19
#include<iostream>
using namespace std;
class myClass{
static int i;
public:
void seti(int a)
{
i=a;
}
int geti()
{
return i;
}
};
int myClass::i;
int main()
{
myClass ob1,ob2;
cout<<myClass::i<<endl;
ob1.seti(200);
cout<<ob1.geti()<<endl;
cout<<ob2.geti()<<endl;
}
Here the issue is that member attributes of a class are private by default. That means that i is private and that you can't access it from outside your class.
One solution would be to declare i as a public member.
class myClass{
public:
static int i;
void seti(int a)
{
i=a;
}
int geti()
{
return i;
}
};
PS : members of a struct are public by default
Related
Can I do an defined constructor which contains an object from another class?
If i can do how is defined.
this is an example.I do classes and how could be defined the constructor of class "Abonati" which contains an object "abonament"
I need that because i have another class which contain a vector of "abonati"
#pragma once
#include"abonament.h"
#include<iostream>
#include<vector>
using namespace std;
class abonati
{
char*nume_abonat;
int nr_telefon;
char *numefisier;
abonament *a;
public:
abonati();
abonati(char*, int , char *,abonament *);
abonati(abonati&a);
void Send();
~abonati();
};
`#pragma once
#include"abonati.h"
class abonament
{
protected:
int cost;
public:
abonament();
abonament(int costa);
virtual ~abonament();
};
#include "abonament.h"
abonament::abonament()
{
this->cost = 0;
}
abonament::abonament(int costa)
{
this->cost = costa;
}
abonament::~abonament()
{
}
`
I guess you would like to pass an class instance to another class constructor
Here is an example
#include <iostream>
class A
{
public:
A(int value) : m_int(value) {}
int GetInt() { return m_int; }
private:
int m_int;
};
class B
{
public:
B(A& a) : m_int(a.GetInt()) {} // Here constructor expects instance of class A
int GetInt() { return m_int; }
private:
int m_int;
};
int main()
{
A a(2);
B b(a); // Pass an object of class A to constructor of class B
std::cout << b.GetInt() << std::endl;
return 0;
}
Prints
2
I have a class with a private static variable. The main function should change the value in the variable, but even if I set the main function as a friend of the class the compiler tells me that the variable is private and not accessible from main.
Example:
ClassA.h:
namespace nameA{
class ClassA {
private:
static int varA;
public:
ClassA(){};
friend int main(void);
};
}
ClassA.cpp:
namespace nameA{
int ClassA::varA = 0;
}
main:
int main(void){
ClassA::varA = 42; //ERROR
}
I don't know if "friend" also allows access to static members or if I have to find another solution.
It because friend function main in ClassA is located in nameA namespace.
If you want to declare as friend the int main(void) function, that located in global scope, you should do it this way:
friend int ::main(void);
Whole source (compiled in VS2015):
int main(void);
namespace nameA {
class ClassA {
private:
static int varA;
public:
ClassA() {};
friend int ::main(void);
};
}
namespace nameA {
int ClassA::varA = 0;
}
int main(void) {
nameA::ClassA::varA = 42;
return 0;
}
Your friend declarations grants friendship to a function named main in the namespace nameA, not the global main function.
Your code is equivalent to
namespace nameA
{
int main(void);
class classA
{
...
friend int main(void);
};
}
You need to declare main before the namespace starts.
int main(void);
namespace nameA
{
class classA
{
...
friend int main(void);
};
}
should work
I am super-new to classes and still wrapping my brain around how they work. Any help/advice/pointers-> are appreciated!
I have two classes. Within the second class is an array of the first class. I am trying to assign values to the private member variables contained in the array of the first class.
I get this error message when compiling:
hw2Test.cpp: In member function 'void bar::set(int)':
hw2Test.cpp:11:7: error: 'int foo::x' is private
int x;
^
hw2Test.cpp:34:12: error: within this context
foodoo[0].x = x;
^
Here is the code:
#include <iostream>
using namespace std;
class foo
{
public:
private:
int x;
};
class bar
{
public:
void set(int x);
private:
foo foodoo[1];
};
int main()
{
bar tar;
tar.set(1);
return 0;
}
void bar::set(int x)
{
foodoo[0].x = x;
}
foo::x is declared as private, so only methods of foo can access it. But you are trying to access x inside of a method of bar instead, which does not have access to foo's private members.
To give bar access, you need to either:
declare foo::x as public:
class foo
{
public:
int x;
};
void bar::set(int x)
{
foodoo[0].x = x;
}
declare a public setter:
class foo
{
public:
void set(int i);
private:
int x;
};
void foo::set(int i)
{
foodoo[0].x = i;
}
void bar::set(int x)
{
foodoo[0].set(x);
}
declare bar as a friend of foo:
class foo
{
public:
private:
int x;
friend class bar;
};
void bar::set(int x)
{
foodoo[0].x = x;
}
class A{
int _a;
public:
/--/ void setfunc(int a) ............. will static works here
{
_a=a;
}
int getValue(){return _a};
};
class B{
public:
void func()
{
/--/ setfunc(1); ...................Dont want to create object of A.
}
};
class C{
public:
void something()
{
A aa;
cout<<aa.getValue(); ............. want a value update by class B setfunc
}
};
int main()
{
B bb;
bb.func();
C cc;
cc.something();
}
Question : How can setfunc() can be called in another function without using that class object. Also, if it changes like setting value of "_a" via someclass B. the same value of will persist whenever I try to retrieve it in someanother class like C via getValue()
In static function you can use only static members of class. Like this (_a is static):
class A {
static int _a;
public:
static void setfunc(int a)
{
_a=a;
}
static int getValue(){return _a};
};
Otherwise, you can't do anything with non-static members:
class A {
int _a;
public:
static void setfunc(int a)
{
_a=a; // Error!
}
static int getValue(){return _a}; // Error!
};
I have 2 classes such as below:
class A : public C
{
void Init();
};
class B : public C
{
int a = 3;
};
How can i access the member of class B in class A?
class A::Init()
{
class B. a ???
}
Perhaps you want a static member, which you can access without an object of type B?
class B : public C
{
public:
static const int a = 3;
};
Now you can access it from anywhere (including your A::Init function) as B::a.
If that's not what you want, then please clarify the question.
You must create an instance of the class B and declare the variable public
class B : public C
{
public:
int a=3;//Data member initializer is not allowed in Visual Studio 2012, only the newest GCC
};
void A::Init()
{
B b;
b.a= 0;
}
If you don't want to create an instance of class B, declare the variable static
class B : public C
{
public:
static int a = 3;
};
And then access it like this:
void A::Init()
{
B::a=0;
}
if you want access a "property" of class B as global, not a particular object then make variable static in this class
class B : public C
{
public:
static const int a = 0;
};
and access it using B::a
alternatively:
class A{
public:
void init(){
static int a2 = 0;
a1=a2;
}
int a(){return a1;}
private:
static int a1;
};
int A::a1=0;
You can also try to add static function in your class. But I'm not sure if this what you
are looking for.
class B: public C
{
public:
static int GetA(void) {return a;}
private:
static int a;
};
int B::a = 4;
class A: public C
{
public:
void Init()
{
std::cout<<"Val of A "<<B::GetA()<<std::endl;
}
private:
int b;
};