OOP Week 1-2
OOP Week 1-2
int getSide()
{ return side; }
Square object’s functions: setSide - set the size of the side of the square,
class Car {
public:
Access string brand;
specifiers string model;
int year;
};
int main() {
MyClass myObj; // Create an object of MyClass
int main() {
// Create an object of Car
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
class Square
{
private:
int side;
public:
void setSide(int s)
inline
functions
{ side = s; }
int getSide()
{ return side; }
};
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-22
Defining Member Functions After the
Class Declaration
class Square
{
private: Has no
int side; parameters
public:
Square() // default
{ side = 1; } // constructor
// Other member
// functions go here
};
class Square
{ Has parameter
private: but it has a
int side; default value
public:
Square(int s = 1) // default
{ side = s; } // constructor
// Other member
// functions go here
};
struct Student
structure tag
{
int studentID;
string name; structure members
short year;
double gpa;
}; Notice the
required
;
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-48
struct Declaration Notes
gpa
struct Dimensions
{
int length,
width,
height;
// Constructor
Dimensions(int L, int W, int H)
{length = L; width = W; height = H;}
};