90 likes | 97 Views
This chapter explores the fundamental data types in C++, including ints, doubles, chars, bools, and enumerations. It also discusses the differences and similarities between structs and classes, as well as the advantages of using structs and classes in object-oriented programming.
E N D
Data Structures and ADTs Chapter 3
C++ Data Types • Fundamental(or simple or scalar): • Data objects are single objects • int, double, char, bool, complex, • the related types (unsigned, short, etc.) • enumerations • Structured: • Collections of data • arrays, structs, unions, classes, valarrays, bitsets, the containers and adapters in STL
Structs vs. Classes Similarities • Essentially the same syntax • Both are used to model objects with multiple attributes (characteristics) • represented as data members • also called fields … or … • instance or attribute variables). • Thus, both are used to process non-homogeneous data sets.
No classes in C Members public by default Can be specified private Both structs and classes in C++ Structs can have members declared private Class members are private by default Can be specified public Structs vs. ClassesDifferences
Advantages in C++(structs and Classes) • C++ structs and classes model objects which have: • Attributes represented as data members • Operations represented as functions (or methods) • Leads to object oriented programming • Objects are self contained • "I can do it myself" mentality • They do not pass a parameter to an external function
Designing a Class • Data members normally placed in private: section of a class • Function members usually in public: section • Typically public: section followed by private: • although not required by compiler
Rationale Data members are typically private • Cannot be accessed outside class • Application programs must interact with an object through its interface • Public member functions control interaction between programs and class. • Application programs need not know about implementation!
Separate Implementation Details from Application Code • If NOT … change in implementation forces change in the application code • Increased upgrade time • Increased programmer cost • Decreased programmer productivity • Reduced profits due to • Delayed releases/upgrades • Loss of customer confidence in software reliability
Traditional Implementation • Specification of class in .h fileImplementation in .cpp file • Forces data abstraction • separates interface from interface details • Increased use of templates changes this • specification and implementation must be in same file • thus the dropping of the .h from standard class libraries • they are really class-template libraries