×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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.

C++ - typedef a class

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

Declaration

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.

Example of typedef a class

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.