Data Structures

Can We Place A Data Structure Inside A Class?
1
2
3
4
5
6
7
8
9
10
11
12
13
14

class Test
{
public:
	Test(struct mv{};):movie(mv)
	{}
private:
	struct movie
	{
		int year;
		string name;
	};
}
Last edited on
You can, but here you treat movie as a member while it is really a typename. It should be
1
2
3
4
5
6
7
8
9
10
11
class Test
{
public:
   Test(struct movie mv) : my_movie(mv) {}//I don't know if you need that "struct" here.
private:
   struct movie
   {
      int year;
      string name;
   } my_movie;
};
 
Test(struct mv{};)

you can't do this. that's illegal...
Nesting a class (or struct) inside another class really only changes it's name. It's sort of like putting a class inside a namespace. Think of the inner class as a static member of the outer class, that is, each instance of the Outer does not have it's own definition for Inner (or Inner's members.)
1
2
3
4
5
6
7
8
9
10
namespace Space
{
	class Outer //It's full name is Space::Outer
	{
		class Inner //It's full name is Space::Outer::Inner
		{
			...
		};
	};
}
if i put structure outside the class is it right? plz help this is wht i did
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

enum religions{buddhist,catholic,hindu,muslim,other};
enum genders{male,female};



class Employee
{
public:
	Employee()//i dont know how to write the constructor
	{
	
	}
private:
	struct EmployeeDetails
	{
		char staffNum[];
		char name[];
		char nic[];
		genders gender;
		char designation[];
		char department[];
		char dateJoined[];
		char nationality[];
		religions religion; 
	};



}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Employee
{
 private:
	struct EmployeeDetails
	{
		char staffNum[];
		char name[];
		char nic[];
		genders gender;
		char designation[];
		char department[];
		char dateJoined[];
		char nationality[];
		religions religion; 
	} details; //Create a private member variable of type EmployeeDetails.

 public:
	Employee() {
		//Initialize your EmployeeDetails structure
		details.staffNum = ...
		details.name = ...
		...
	}

	//Add an accessor method to get the private member details.
	EmployeeDetails getDetails() const { return details; }

	//Another possible accessor method might look like:
	// const EmployeeDetails& getDetails() const;
};

int main() {
	Employee em;
	em.getDetails.name; //get employee em's name
	em.getDetails.gender; //get employee em's gender
	...
}
char staffNum[]; That's not valid, is it?
Whoops, wasn't really looking at what he had in the struct in terms of data type. Yea, that (those) needs to be changed to char *staffNum;, or const char *staffNum; in which case you would need to set it (them) via an initializer list.
You're confusing const char* with char* const. Also, there is the obvious option of char staffNum[80];.
Yea, what you said... Or we could go with the also obvious string... ;)
Topic archived. No new replies allowed.