Upload
ellen-russell
View
212
Download
0
Embed Size (px)
Citation preview
Class and Structure
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.
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
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 }