📜  C++中的类vs结构vs联合

📅  最后修改于: 2021-05-30 16:31:25             🧑  作者: Mango

Class 它是用户定义的数据类型,其中包含变量和函数。这就像一个对象的蓝图。默认情况下,班级成员是私人的。例如,汽车是一个对象,其颜色,设计,重量是其属性,而制动器,限速等是其功能。

句法:

class 
{
     private:
     // Data members
     ........... ...... .......
     
     public:
     // Data members
     // Member function
     ......... ........ ......
   
     protected:
     // Data members
     // Member function
    ....... ....... ......
};

下面是C++程序来说明该类:

C++
// C++ program to illustrate class
#include 
using namespace std;
  
// Class with data members
// and member function
class Geeks {
public:
    string geekname;
    int roll_no;
  
    // Function printName
    // prints the name
    void printName()
    {
        cout << "Geekname is: "
             << geekname;
    }
  
    // Function printRollno
    // prints the roll no
    void printRollno()
    {
        cout << "Roll no  is: "
             << roll_no;
    }
};
  
// Driver Code
int main()
{
  
    // Object of class Geeks
    Geeks obj1;
  
    // Initialize a public data
    // members of class Geeks
    obj1.geekname = "Geek";
    obj1.roll_no = 15;
  
    // Function Call
    obj1.printName();
    cout << endl;
  
    // Function Call
    obj1.printRollno();
  
    return 0;
}


C++
// C++ program to illustrate structure
#include 
using namespace std;
  
// Structure with data members
// and member function
struct geekyPerson {
  
    // Data members
    string name;
    int age;
    float salary;
  
    // Member function to display
    void display()
    {
        cout << "Name: " << name
             << endl;
        cout << "Age: " << age
             << endl;
        cout << "Salary: " << salary;
    }
};
  
// Driver Code
int main()
{
    // Instance of the structure
    geekyPerson g1;
  
    // Accessing the data by the
    // dot operator
    g1.name = "Geeky";
    g1.age = 23;
    g1.salary = 15000;
  
    // Function Call
    g1.display();
  
    return 0;
}


C++
// C++ program to illustrate union
#include 
using namespace std;
  
// Union declaration with the
// data members
union geeks {
    int id;
    int salary;
    char name[30];
    int age;
};
  
// Driver Code
int main()
{
    // Instance of the union
    union geeks g1;
  
    g1.id = 1;
    cout << "Id  : " << g1.id
         << endl;
  
    strcpy(g1.name, "Geeky");
    cout << "Name : " << g1.name
         << endl;
  
    g1.salary = 35000;
    cout << "Salary : " << g1.salary
         << endl;
  
    g1.age = 25;
    cout << "Age : " << g1.age
         << endl;
  
    return 0;
}


输出:
Geekname is: Geek
Roll no  is: 15

说明:在上面的C++类示例中,Geeks在公共访问说明符中具有数据成员和成员函数。使用类的对象使用某些值初始化数据成员。使用该对象,将调用该类的功能。

结构这是对变量进行分组的便捷方法。默认情况下,结构成员是公共成员。可以使用结构来有效地提高抽象级别,以改进代码。

句法:

struct  
{
    // Data members
    ....... ...... ......
       
    // Member functions
    ....... ...... ......
};

下面是C++程序来说明结构:

C++

// C++ program to illustrate structure
#include 
using namespace std;
  
// Structure with data members
// and member function
struct geekyPerson {
  
    // Data members
    string name;
    int age;
    float salary;
  
    // Member function to display
    void display()
    {
        cout << "Name: " << name
             << endl;
        cout << "Age: " << age
             << endl;
        cout << "Salary: " << salary;
    }
};
  
// Driver Code
int main()
{
    // Instance of the structure
    geekyPerson g1;
  
    // Accessing the data by the
    // dot operator
    g1.name = "Geeky";
    g1.age = 23;
    g1.salary = 15000;
  
    // Function Call
    g1.display();
  
    return 0;
}
输出:
Name: Geeky
Age: 23
Salary: 15000

说明:在结构名称之前使用struct标记来定义结构。每个成员定义都是一个普通的变量定义,例如int i;或浮动f;或任何其他有效的变量定义。使用该对象,将调用结构的功能。

联合这是用户定义的数据类型。它类似于结构。联合的所有数据成员共享相同的存储位置。工会的规模基于工会中最大成员的规模。若要将相同的内存位置用于两个或多个成员,则最好是并集。

句法:

union 
{
    // Data members
    ..... ....... .........
} union_variables;
 
where,
union_name − Name was given to the union.
union_variable − The object of union.
Data members -Member variables.

下面是说明联合的C++程序:

C++

// C++ program to illustrate union
#include 
using namespace std;
  
// Union declaration with the
// data members
union geeks {
    int id;
    int salary;
    char name[30];
    int age;
};
  
// Driver Code
int main()
{
    // Instance of the union
    union geeks g1;
  
    g1.id = 1;
    cout << "Id  : " << g1.id
         << endl;
  
    strcpy(g1.name, "Geeky");
    cout << "Name : " << g1.name
         << endl;
  
    g1.salary = 35000;
    cout << "Salary : " << g1.salary
         << endl;
  
    g1.age = 25;
    cout << "Age : " << g1.age
         << endl;
  
    return 0;
}
输出:
Id  : 1
Name : Geeky
Salary : 35000
Age : 25

说明:同样,在并集中,对象也用于访问数据成员和成员函数。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”