• Atom Feed

    Create Atom feed for your website in minutes
  • C Code Auto Indent

    Auto indent your entire C project with a simple shell command
  • C file ops

    Can you perform insert operation using C file operations? Check this C code and C file operations concepts
  • C posix signals

    Everything on Linux Signals and Signal handling, with sample code
  • Django MVC Framework

    Setting up Python Django website on Apache Shared web hosting server
  • FTP

    FTP Unix Command with examples
  • GlusterFS

    Concepts, Installation and Performance Benchmarking Reports
  • File Append in Java

    Program to perform File Append operation in Java
  • Essential Linux Commands

    Learn the basic essential linux commands in minutes
  • Loop Device in Linux

    Create loop device in Linux
  • PostgreSQL Database

    Installation and Startup Script
  • Pthreads - POSIX Multithreading

    APIs, Concepts and Sample Programs
  • Python Ctypes Module

    Intermixing C with Python and extending Python, with sample programs
  • Socket Programming

    Socket Programming in Linux explained, with TCP and UDP examples
  • String Functions in C

    Did you know string manipulation in C is so easy?
Home CPP Interview Questions

Explain difference between a structure, union and class in C++

Classes vs structures in C++:

Classes and structures in C++ are same except class defaults to private and structures to public.

Both have data members, member function, this pointer, static member functions.
 

Difference between union, classes and structures in C++:

Union is also similar to classes and structures except:

1. unions cannot be used as base or derived class in inheritance
2. unions cannot have static members
3. defaults to public


Sample program showcasing unions in C++ similarities with classes in C++:

union personalInfo
{

  int id;
  char name[20];
  void print()
  {
    cout<<this->id;
  }

};

 

int main()
{
  union personalInfo me;
  me.id=10;
  me.print();
  return 0;
}