This document covers the concepts of structures and classes in C++, detailing their definitions, member functions, and differences. It explains how to declare and initialize structures, access their members, and the principles of object-oriented programming, including encapsulation and data abstraction. The document emphasizes the importance of public and private members in classes and the distinction between structures and classes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views37 pages
684198
This document covers the concepts of structures and classes in C++, detailing their definitions, member functions, and differences. It explains how to declare and initialize structures, access their members, and the principles of object-oriented programming, including encapsulation and data abstraction. The document emphasizes the importance of public and private members in classes and the distinction between structures and classes.
Accessing Structure Members Dot Operator to access members account.balance account.interestRate account.term Called ‘member variables’ The ‘parts’ of the structure variable Different structs can have same name member variables No conflicts
Structure Assignments Given structure named CropYield Declare two structure variables: CropYield apples, oranges; Both are variables of ‘struct type CropYield’ Simple assignments are legal: apples = oranges; Simply copies each member variable from apples into member variables from oranges
Structures as Function Arguments Passed like any simple data type Pass-by-value Pass-by-reference Or combination Can also be returned by function Return-type is structure type Return statement in function definition sends structure variable back to caller
Initializing Structures Can initialize at declaration Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003}; Declaration provides initial data to all three member variables
Classes Similar to structures Adds member FUNCTIONS Not just member data Integral to object-oriented programming Focus on objects Object: Contains data and operations In C++, variables of class type are objects
Dot and Scope Resolution Operator Used to specify ‘of what thing’ they are members Dot operator: Specifies member of particular object Scope resolution operator: Specifies what class the function definition comes from
A Class’s Place Class is full-fledged type! Just like data types int, double, etc. Can have variables of a class type We simply call them ‘objects’ Can have parameters of a class type Pass-by-value Pass-by-reference Can use class type like any other type!
Principles of OOP Information Hiding Details of how operations work not known to ‘user’ of class Data Abstraction Details of how data is manipulated within ADT/class not known to user Encapsulation Bring together data and operations, but keep ‘details’ hidden
Public and Private Members Data in class almost always designated private in definition! Upholds principles of OOP Hide data from user Allow manipulation only via operations Which are member functions Public items (usually member functions) are ‘user-accessible’