public class member

I am trying to send some variable into a public class member.
I have 3 variables say x, y ,z.
I want to have them as a class' member. Then i will restore them at a list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class sınıf{
public
float num1 ;
float num2 ;
float num3;
};
..
void main(){
vector<sınıf<list;
cin>> x;
cin>> y;
cin>>z;

			
sınıf1= new sınıf;
//here i think i need some lines sending x y and z to num1 num 2 and num 3;
list.push_back(*sınıf1);


}


There is no reason to be using new here. You can assign the members one at a time like this:
1
2
3
4
5
sınıf sınıf1;
sınıf1.num1 = x;
sınıf1.num2 = y;
sınıf1.num3 = z;
list.push_back(sınıf1);

If x, y and z are of type float you can intialize the object in one line:
1
2
sınıf sınıf1 = {x, y, z};
list.push_back(sınıf1);

In C++11 you write it all in one statement:
list.push_back({x, y, z});
or
list.emplace_back(x, y, z);
Last edited on
thank you.

How will i call these variable then?

For example assume i got lots of classes sınıf1s and in a for while i send them to the list. I want exactly 14.class y variable and make some calculations with this variabe. how will i have it?
sınıf is a class. sınıf1 is an object. When you pass an object to push_back a copy of the object is made that will be stored in the vector. The objects stored in the vector doesn't have variable names (you cannot use name sınıf1 to access them). There are a few different ways to access the objects stored in a vector but one of the most common is to use an index to access the objects the same way you do with arrays. list[0] will access the first element, list[1] the second and so on. You can get the number of elements in the vector by using the size member function: list.size()
Last edited on
Topic archived. No new replies allowed.