C++ 使用结构而不是类

C++ 使用结构而不是类,c++,C++,我从未上过课,但我确实明白了大概的意思,我正在尝试。 下面的代码在网上找到,使用链表生成堆栈,我想知道我们是否使用了结构而不是类,这个程序会是什么样子 #include<iostream> using namespace std; // Creating a NODE Structure struct node { int data; struct node *next; }; // Creating a class STACK class stack { struct n

我从未上过课,但我确实明白了大概的意思,我正在尝试。 下面的代码在网上找到,使用链表生成堆栈,我想知道我们是否使用了结构而不是类,这个程序会是什么样子

#include<iostream>
using namespace std;


//   Creating a NODE Structure
struct node
{
int data;
struct node *next;
};

// Creating a class STACK
class stack
{
struct node *top;
public:
stack() // constructure
{
    top=NULL;
}
void push(); // to insert an element
void pop();  // to delete an element
void show(); // to show the stack
};
// PUSH Operation
void stack::push()
{
int value;
struct node *ptr;
cout<<"nPUSH Operationn";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
    ptr->next=top;
top=ptr;
cout<<"nNew item is inserted to the stack!!!";

}

// POP Operation
void stack::pop()
{
 struct node *temp;
 if(top==NULL)
 {
    cout<<"nThe stack is empty!!!";
 }
 temp=top;
 top=top->next;
 cout<<"nPOP Operation........nPoped value is "<<temp->data;
 delete temp;
 }

// Show stack
void stack::show()
{
struct node *ptr1=top;
cout<<"nThe stack isn";
while(ptr1!=NULL)
{
    cout<<ptr1->data<<" ->";
    ptr1=ptr1->next;
}
cout<<"NULLn";
}

// Main function
int main()
{
stack s;
int choice;
while(1)
{
    cout<<"n-----------------------------------------------------------";
    cout<<"nttSTACK USING LINKED LISTnn";
    cout<<"1:PUSHn2:POPn3:DISPLAY STACKn4:EXIT";
    cout<<"nEnter your choice(1-4): ";
    cin>>choice;
    switch(choice)
    {
        case 1:
            s.push();
            break;
        case 2:
            s.pop();
            break;
        case 3:
            s.show();
            break;
        case 4:
            return 0;
            break;
        default:
            cout<<"Please enter correct choice(1-4)!!";
            break;
     }
  }
  return 0;
}
#包括
使用名称空间std;
//创建节点结构
结构节点
{
int数据;
结构节点*下一步;
};
//创建类堆栈
类堆栈
{
结构节点*top;
公众:
stack()//结构
{
top=NULL;
}
void push();//插入元素的步骤
void pop();//删除元素的步骤
void show();//显示堆栈
};
//推送操作
void stack::push()
{
int值;
结构节点*ptr;
coutnext=NULL;
如果(顶部!=NULL)
ptr->next=顶部;
top=ptr;

coutStruct几乎像一个类。在Struct元素中,默认值是公共的,在类中,它们是私有的。

考虑下面这个小例子:

class TestClass
{
   int foo(void)
   {
      return (a * a);  // is equal to: return (this->a * this->a);
   }

   int a;
};


TestClass example;

example.foo();   // execute function foo of class TestClass with data of instance example
使用struct重写上述内容的方式如下:

struct TestClass 
{
   int a;
}

/* "struct implementation" of foo function */
int TestClass_foo(struct TestClass *this)
{
   return (this->a * this->a);
}

struct TestClass example;

TestClass_foo(&example);   // execute function TestClass_foo with data of example
因此,让我们注意一下在上述示例中使用结构时的区别:

  • 您必须向每个函数调用
    TestClass\u foo
    传递指向结构实际实例的指针
  • 像“TestClass_foo”这样的函数命名很快就会变得混乱
因此,在这个简单的例子中,一个类的好处看起来并不令人印象深刻,但通过这个例子,您可以了解基本的区别


<>但是一旦你开始使用继承或成员可见性(公共的、私有的、受保护的),你就不能使用Struts(或者只是巨大的开销)来做它。但是这就是C++所要做的,所以你不必考虑编译器如何使你的代码工作。

你应该学习C++的一本书,而不是看“在线找到的代码”。。您在这里发现的代码非常糟糕,无法通过任何专业代码审查。至于您的问题,您尝试过吗?发生了什么?默认的基类访问级别也不同。这个答案充满了技术错误和错误。我不知道从何处开始。您似乎没有意识到
在C++中,结构< <代码>几乎相同。<代码>结构> <代码>可以完成<代码>类< /代码>。您可以使用<代码>公共<代码>、<代码>保护< /COD>和<代码>私下<代码>,没有“开销”。你不需要任何指针。ChristianHackl:你完全正确。我觉得我的答案有点不清楚。我在C++中不使用结构。当我看到结构时,我想到C。你可以用我的第二个例子来“伪造”简单的类,但是这很快就会变得混乱。