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

Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.