4
Class and Structure

Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor

Embed Size (px)

Citation preview

Page 1: Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor

Class and Structure

Page 2: Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor

2

StructureDeclare using the keyword structpurpose is to group dataDefault visibility mode is publicA struct doesn't have a constructor or destructorCannot support overloaded operators.Cannot use C++-specific features such as

inheritance and member functions.A structure couldn't be null like a class.

Page 3: Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor

3

ClassDeclare using the keyword classA class should be used for grouping data and methods

that operate on that dataThe class default access type is privateClasses have a public and private membersThe default mode for inheritance is private. classes are used to encapsulate data and the functionsClass support constructors and destructorsClass support operator overloading & function

OverloadingClass can be declare al null

Page 4: Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor

4

Class Vs Structure• Members of a class are private by default and

members of struct are public by default • class Test {• int x; // x is private• };• void main()• {• Test t;// class boject• t.x = 20; // compiler error • // because x is private• }

struct Test { int x; // x is public};void main(){ Test t; t.x = 20; // works fine because x is public }