সোমবার, ১৬ অক্টোবর, ২০১৭

What is the differentiate between structure and class ?

The differentiate between structure and class 

                            1. Members of a class are private by defaults and members of a structure are public by defaults . Examples

class Sabbir
{
 int s;
};
int main( )
{
    Sabbir t;
  t.s=40;
getchar( );
return 0;
}
If you run this program computer shows error because here  s is private .

 struct Sabbir
{
 int s;
};
int main( )
{
    Sabbir t;
    t.s=40;
   getchar( );
   return 0;
}
If you run this program compiler work properly  because here  s is public .

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 specifier is private .

Example

class Sabbir
{
 int s;
};
class derived : Sabbir { };
int main( )
{
    Derived d;
 
    d.s=40;
   getchar( );
   return 0;
}

Here the coding is not working beacuse compiler shows error inheritance is private .

class Sabbir
{
 int s;
};
struct derived : Sabbir { };
int main( )
{
    Derived d;
 
    d.s=40;
   getchar( );
   return 0;
}

Here the coding is  working beacuse compiler shows that inheritance is public.



Share:

0 comments:

একটি মন্তব্য পোস্ট করুন