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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.