How to define class and object and accessing data members in C++

1 Answer

0 votes
#include <iostream>
#include <cstring>

using namespace std;

class Worker
{
	public:
		char name[30];
		float age; 
		char profession[30];  
};

int main()
{
	Worker w1;
	
	w1.age = 37;
	strcpy(w1.name, "Tori"); // C-Style String
	strcpy(w1.profession, "Actor"); // C-Style String
	
	cout << "Name: " << w1.name << " Age: " << w1.age << " Profession: " << w1.profession;

	return 0;
}


/*
run:

Name: Tori Age: 37 Profession: Actor

*/

 



answered Feb 20, 2016 by avibootz

Related questions

1 answer 245 views
1 answer 183 views
1 answer 150 views
1 answer 179 views
1 answer 159 views
...