Showing posts with label struct. Show all posts
Showing posts with label struct. Show all posts

Wednesday, November 21, 2012

Something about Class And Struct in cpp

#include <iostream>
using namespace std;

class A{
    public:
        int a;
};

struct B:A{};

struct C{
    int c;
};

class D:C{};

int main()
{
    B b;
    D d;
    b.a = 1;
    d.c = 2;  //give error at this line
}

OUTPUT:

Something to Remember:
  • In c# struct goes on stack means they are value type. and class is reference type. So there is huge difference between struct and class in c# but in C++ there is almost no difference between a struct and  a class (beside that public and private thing).

Friday, June 8, 2012

Difference between class and struct in cpp

1) Members of a class are private by default and members of struct are public by default. For example program 1 fails in compilation and program 2 works fine.

// Program 1 to show we can't access the class data directly
#include <iostream>
using namespace std;

class Demo 

{
    int x;         // x is private (by default)
};

int main()
{
   Demo d;
   d.x = 20;      // compiler error because x is private
   cout << d.x ;
}

// Program 2 to show we can access struct data directly
#include <iostream>
using namespace std;

struct Demo
{
    int x;       // x is public (by default)
};

int main()
{
  Demo d;
  d.x = 20;    // works fine because x is public
  cout << d.x ;
}


2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private. For example program 3 fails in compilation and program 4 works fine.

// Program 3 to show inheritance between classes is, private by-default
#include <iostream>
using namespace std;

class Base

{
   public:
      int x;
};

class Derived : Base { };  
  // is equilalent to class Derived : private Base {}

int main()
{
  Derived d;
  d.x = 20;        // compiler error becuase inheritance is private
  cout << d.x ;
}

// Program 4 to show inheritance between structure is, public by-default
#include <iostream>
using namespace std;

struct Base 

{
    public:
       int x;
};

struct Derived : Base { };   // is equilalent to struct Derived : public Base {}

int main()
{
   Derived d;
   d.x = 20;       // works fine becuase inheritance is public
   cout << d.x ;
}


Note: struct in c# doesn't  support inheritance.

Monday, May 14, 2012

'c++ struct' can have private_members and function also

//A Simple program to show c++ structure can have function
// using c++, GCC complier
 
#include <iostream>
using namespace std;

struct CRectangle       //here we define a user-defined-datatype using 'struct'
{
    private:
        int x, y;


    public:
        void set_values (int,int);


        int area()
        {
            return (x*y);
        }
};

void CRectangle :: set_values (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    CRectangle rect;    //creating the object (rect) of a class (CRectangle)


    rect.set_values (3,4);
    cout << "area:" << rect.area();
   
    cin.ignore();
    cin.get();
}

______________________________________________________________
also see the same program using class (only difference, we use 'class' instead of 'struct')

Thursday, April 5, 2012

A simple 'struct' program

// simple struct (user defined datatype) program
// using c++


#include <iostream>
using namespace std;

struct student                 //student is user defind data type
{
    int roll_no;
    char name;
    int age;
};

int main()
{
    student a;              //a is the object of student


    cout << "\nEnter the name of student: ";
    cin >> a.name;


    cout << "\n Enter your age: ";
    cin >> a.age;


    cout << "\n enter your roll no: ";
    cin >> a.roll_no;
   
    cout << "\n student name is :" << a.name;
    cout << "\n               age is :" << a.age;
    cout << "\n               roll no :" << a.roll_no;
   
}