CS Week 3 - Classes I Flashcards
what is a strut
struct name {
type item;
….
};
—- in main —
name object;
object.item;
data member
each struct sub item
member access
dot notation
.
for a declared variable, each struct data member can be accessed using .
how can use structs and functions
to return multiple values
function return type is struct name
pass in a regular argument
create another object in order to return it
how can you use structs and vectors
declaration of just one vector that stores items that each have a name and number data member
how to declare a constructor the professor way
name()
: item(iniValue), …. {
}
name (type item, …)
: item(iniValue),… {
}
how do you use a private data in main
in struct
type getName() const {return Name};
- usually all in one line
in main
cout «_space;getName();
how do you set private data from main
in struct
void setName(type nameInput) {
name = nameInput;
}
in main
object.setName(input);
cout «_space;getName;
difference between classes and structs
struct - default everything is public
class- default everything is private
struct is when you have no accessors or methods/functions needed
what is an object
a grouping of data - variables - and operations that can be performed on that data - functions
what is abstraction
to have a user interact with an item at a high level, with lower level internal details hidden from the user
-information hiding, or encapsulation
what is abstractions data type (ADT)
a data type whose creation and update are constrained to specific well-defined operations - example - class
class
defines a new type that can group data and functions to form an object
public member functions
indicate all operations a class user can perform on the object
private data members
variables that member functions can access but class users cannot
function declaration
return type, function name, and parameter types;
function definition
return type className :: functionName (parameters) {
statements ;}
mutator
modify data
sets the value
accessors
accesses, not to modify
gets the value
const after parameters
if it calls other functions, those must also be const
private helper functions
to help public functions carry out tasks
any in the class can call these
private and public can call public and private
how to initialize data members
in the class definition
…
private :
string name = something;
int num = something;
};
or constructors
what is a constructor
called automatically when a variable of that class type is declared and which can initialize data members
what is a default constructor
no arguments
className :: className () {
set the variables equal to something here;
}
overload
by defining multiple constructors differing in parameter types
class name{
public:
//sets to default value
name ();
//sets to input values when called name (parameters that will be set to the private varibales); ... };