simple class

hello,

i'm still beigner for classes in c++

now i'm trying to write a simple university system which is to create student,subject...etc

now i want to create on;y new subject and please see my code
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
#include<iostream>
using namespace std;

class subject
{
	string code;
 public:
	void create_code();
};

class student
{
	string name;
 public:
	void create_student();
};

int main()
{

create_code();
	return 0;
}

void subject::create_code()
{
	cout <<"Creat a new subject: " <<endl;
	cin >> code;
}

void student::create_student()
{
	cout <<"Creat a new student: " <<endl;
	cin >> name;
}


my problem is in the main function and i want to call the member function for the class subject.

please notice that i'm still beigner

waiting for you
Hi

first you need to create an instance of your class,
and then call the method of your new object

1
2
3
4
5
6
7
8

int     main()
{
        subject*   sub = new subject();

        sub->create_code();
}


Hope this helps
Shredded
hi there,

i tried your code but it doesn't work

is it like this?
1
2
3
4
5
6
7
8
int main()
{
subject*sub = new subject();

    sub->create_code();
create_code();
	return 0;
}

Hi again,

Yeah sorry I didnt test the code what is missing is

#include <string>

at top of file

I would also move code & name in class decleration under either public or private
eg.

1
2
3
4
5
6
7
8
9
10
class subject
{
	
 public:

	void create_code();

private:
	string code;
};


Edit: Keep main as in my first post

It should work this time
Shredded
Last edited on
except that there is no need to work with pointers and dynamic allocation.
And student::create_student or subject::create_code() don't create new instances, but set the state of the object
you guys might be confusing him, I think for a beginner he is trying to use the dot operator.

any basics to call your functions or objects:

WRITE CLASS NAME CLASS OBJECT-CREATED NAME;

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
 subject myobject;

//to access members of the class
//write myobject then .(dot)             call what ever is inside your class
//its would be the same as usual for any public objects only
//just remember your object name and the dot operator 

myobject. create_code();

return 0;
}
hi, i'm beginner too, i dont understand what your codes purpose, but to make it compiled i do a little modification , it's compiled and run well
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
38
39
#include <iostream>
#include <string>
using namespace std;

class subject
{
	string code;
 	public:
	void create_code();
};

void subject::create_code()
{
	cout <<"Creat a new subject: " <<endl;
	cin >> code;
}


//------------------------
class student
{
	string name;
 	public:
	void create_student();
};
void student::create_student()
{
	cout <<"Creat a new student: " <<endl;
	cin >> name;
}
//--------------------
int main()
{
	subject yoursubject;
	student yourstudent;
	yoursubject.create_code();
	yourstudent.create_student();
	return 0;
}


with different class your subject and student are sparated , soo i choose this way to make it simpler, like this :
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
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;


//------------------------
class student
{	
 	public:
 	string code;
	string name;
	void create_student()
		{
			cout <<"Creat a new student: ";
			cin >> name;
		};
	void create_code()
		{
		cout <<"Creat a new subject: " ;
		cin >> code;
	}
}

;
//--------------------
int main()
{
	int counter;
	cout<<"\nHow many student do you want to add ? : ";cin>>counter;
	
	student yourstudent[counter];
	for(int i=0;i<counter;i++)
		{
			yourstudent[i].create_student();
			yourstudent[i].create_code();
			system("cls");
		}
	system("cls");
	cout<<"added student is : ";
	for(int i=0;i<counter;i++)
		{
			cout<<"\n["<<i<<"] "<<yourstudent[i].code;
			cout<<"   "<<yourstudent[i].name;
		}
	cout<<"\n\n\n";
	system("pause");
	return 0;
}


hope this help, if you see something wrong in this code please notice me at cpp.pemula@gmail.com in facebook, sorry for my bad english

thanks for all you guys

it helps me a lot, but now i have a question

after i creating subject and students, then i want to list all the subject or list all the student

or maybe list all the subejects for a specific stuednt

to solve this, shloud i use vector? or just class?
waiting
on modified code above, you will have data packed in an array , look to
student yourstudent[counter];
how many your students are depending to your input assigned to counter.
its like
1
2
3
yourstudent[1]
yourstudent[2]
yourstudent[etc]

my second sample above answer your problem, after creating subject it shows the list, and if you like to see specific student you can use
1
2
3
4
if(student[i].name == "yourchoice")
{
cout>>endl>>student[i].name;
};

simple isnt it ?

Topic archived. No new replies allowed.