Home » C++ programming language

typedef a class to some simple name in C++

Let suppose, you have created a class named student_details, you need to use class student_details to create its object.

We can simple typedef the class declaration/definition to a simple name, then, there will no need to use class student_details each time.

Consider the following class declaration

typedef class student_details
{
	private:
	char name[30];
	int age;
	
	public:
	void getData();
	void dispData();
}student;

Here, student is another name given to class student_details, now we can use student everywhere as class student_details.

Here is an example

#include <iostream>
#include <string.h>
using namespace std;

typedef class student_details
{
	private:
	char name[30];
	int age;
	
	public:
	void getData();
	void dispData();
}student;

//class function definitions
void student::getData()
{
	strcpy(name,"Duggu");
	age=21;
}
void student::dispData()
{
	cout<<"Name: "<<name<<endl;
	cout<<"Age: " <<age <<endl;
}


int main()
{
	student std;
	std.getData();
	std.dispData();
	return 0;
}

Output

Name: Duggu
Age: 21





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.