Design a class Person with private data members

Hello!

I've been given this assignment and to be frank, I haven't understood much part of this in college. I usually understand well by examples which are not given there much. So, now, I have an assignment, the full text of which goes as follows:

Design a class Person with private data members as bellow
name as array of character
age as an integer
gender as a character
address as array of character

public member functions as below:
initData( ) function for initialization of all data member
showData( ) function for display of all data members
getAge( ) function for returning age of a person
getGender ( ) function for returning gender of a person
setAge ( ) function for modifying age of a person.
Design the main function to create a person and invoke it’s member functions.

How can I do it?
Last edited on
Programming is like football: the only way to learn it is to do it. Hit your books and figure out how to declare a class with data and member functions. This is a very basic assignment. You need to learn to do this on your own.

I'm sorry if this doesn't seem helpful, but the longer you delay learning the basics, the more trouble you'll have later on.
> I usually understand well by examples
here's a class Car
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
#include <iostream>
#include <cstring>
class Car{
public:
	//this could be better as a constructor
	void initData(const char brand[], const char code[], const char model[], int odometer);
	void showData() const;
	int getOdometer() const;
	const char* getModel() const;
	void setOdometer(int odometer);
private:
	char brand[42];
	char code[10];
	char model[42];
	int odometer;
};

void Car::initData(const char brand[], const char code[], const char model[], int odometer){
	strcpy(this->brand, brand);
	//...
	this->odometer = odometer;
}

void Car::showData() const{
	std::cout << brand << " - " << code << ' ' << model << '\t' << odometer << "km\n";
}

const char* Car::getModel() const{
	return this->model;
}

void Car::setOdometer(int odometer){
	this->odometer = odometer;
}

//...

int main(){
	Car c;
	c.initData("Toyota", "AE86", "Trueno", 250000);
	c.setOdometer(0); //as good as new
	c.showData();
}
@dhayden

Thank you for the encouragement. I agree with what you say, but, sometimes, there's not enough time to learn. I have to submit the assignment really soon.

Also, I didn't stop trying it out myself. I still am trying out stuff.
@ne555

Wow. This seems pretty mich like my assignment. I'll try tinkering it. Thanks!!
HrishikeshK, tinker away! When you get stuck for more than hour, post the code that you have and ask specific questions. If the code isn't compiling, include the exact error message(s). If it isn't doing what you expect, post (1) the input that you're using, (2) the output that you get, and (3) the output that you expect. the more info you post, the easier it is for us to help you.
Registered users can post here. Sign in or register to post.