程序代写 CS2310 Computer Programming

Computer Science, City University of
Semester B 2021-22
CS2310 Computer Programming

Copyright By PowCoder代写 加微信 powcoder

LT09: Class and Object

Defining classes
Defining member functions & scope resolution operator
Public & private members
Constructors

Class and Object
Class and object are important features of Object-oriented Programming Language (C++, Java, C#)
With class, variables and their directly related functions can be grouped together to form a new data type
It promotes reusability and object-oriented design (not covered in this course)
Object is an instance of class, i.e. class is a blue-print and its product is its object.

Class and Object : Example
int radius;
int width, height;

double getCircleArea(){
return 3.14*radius*radius;
double getRectangleArea(){
return width*height;
double getCirclePerimeter(){
return 2*3.14*radius;
double getRectanglePerimeter(){
return 2*(width+height);

class Circle {
int radius;
double getArea(){
return 3.14*radius*radius;
double getPerimeter(){
return 2*3.14*radius;
class Rect{
int width, height;
double getArea(){
return width*height;
double getPerimeter(){
return 2*(width+height);

Without class/object
With class/object

Class and Object
void main(){
Rect r; //Rect is a class, r is an object of Rect
cout << “Please enter the radius of circle”; cin >> c.radius;
cout << c.getArea(); cout << “Please enter the width and height of a rectangle”; cin >> r.width >> r.height;
cout << r.getArea(); void main(){ cout << “Please enter the radius of circle”; cin >> radius;
cout << getCircleArea(); cout << “Please enter the width and height of a rectangle”; cin >> width >> height;
cout << getRectangleArea(); Without class/object With class/object Class in Computer Programming An abstract view of real-world objects, e.g. car, horse Computer program is a model of real-world problem Simple problem: program with variables and functions Large scale program: class and object definition of program component consists of member variables and member functions Member variable : variable belong to class Member function: function primary designed to access/manipulate the member variable of the class An instance of class / runtime representation of a class Class:Robot Class in programming Member variable Member functions int modelNum; int width; int height; int powerLevel; void start(); void shutdown(); void moveForward(int step); void turnLeft(int degree); void turnRight(int degree); void takePhoto(); ………………………………….. What is an object? Class: Circle int radius; int color; Member functions Color:orange Color: blue Color: green Objects of Circle Classes and Objects in C++ A class is a data type, objects are variables of this type An object is a variable with member functions and data values cin, cout are objects defined in header
C++ has great facilities for you to define your own class and objects

Defining classes
class class_name {
public / protected / private:
attribute1 declaration;
attribute2 declaration;
method1 declaration;
method2 prototype;
return_value classname::method2{
method body statement;

Defining classes (example I)
#include
using namespace std;
class DayOfYear
int month;
void output(){
cout << "month = " << month; cout << ", day = " << day << endl; Member variables Member method/function Member function In C++, a class definition commonly contains only the prototypes of its member functions (except for inline functions) Use classname::functionName to define the member function (method) of particular class. class Circle int radius; double getArea(); double Circle::getArea(){ return 3.1415*radius*radius; Defining classes (example II) #include
using namespace std;
class DayOfYear
void output(); //member func. prototype
int month;
void DayOfYear::output()
cout << "month =" << month << ", day =" << day << endl; Define the method elsewhere Create object, access its function To declare an object of a class Class_name variable_name; Circle c1,c2; DayofYear today; A member function of an object is called using the dot operator: today.output(); c1.getArea(); Create pointer and access its function We can also declare the pointer of a class Class_name* pointer_name; Circle *c1, *c2; DayofYear *today; A member function of the pointer is called using the arrow operator (->):
today->output();
c1->getArea();

Main function
void main()
DayofYear today, birthday;
cin >> today.month >> today.day;
cin >> birthday.month >> birthday.day;
cout << "Today’s date is: "; today.output(); cout << "Your birthday is: "; birthday.output(); if (today.month == birthday.month && today.day == birthday.day) cout << " !\n"; Public and private members By default, all members of a class are private You can declare public members using the keyword public Private members can be accessed only by member functions (and friend functions) of that class, i.e. only from within the class, not from outside A new class definition for DayOfYear class DayOfYear void input(); void output(); void set(int new_m, int new_d); int get_month(); int get_day(); bool valid(int m, int d); // check if m,d valid int month; Member function definitions bool DayOfYear::valid(int m, int d) if (m<1 || m>12 || d<1) return false; switch(m){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: return d<=31; break; case 4: case 6: case 9: case 11: return d<=30; break; return d<=29; break; Member function definitions void DayOfYear::input() // input and validate cout << "Enter month and day as numbers: "; cin >> m >> d; // local var. of input()
} while (!valid(m,d));

month = m; // accessing private members

Member function definitions
void DayOfYear::set(int new_m, int new_d)
if (valid(new_m, new_d)) {
month = new_m;
day = mew_d;

int DayOfYear::get_month()
{ return month;

int DayOfYear::get_day()
{ return day;

A new main program
void main()
DayOfYear today, birthday;

today.input();
birthday.input();
cout << "Today’s date is:\n"; today.output(); cout << “Your birthday is:\n”; birthday.output(); if (today.get_month()==birthday.get_month() today.get_day() == birthday.get_day()) cout << “ !\n”; Private Variables and Access functions Member functions that give you access to the values of the private member variables are called access functions, e.g., get_month, set Useful for controlling access to private members: E.g. Provide data validation to ensure data integrity. Needed when testing equality of 2 objects. (The predefined equality operator = = does not work for objects and variables of structure type.), e.g. obj1==obj2 (not work!) Why private variable? Prevent others from accessing the variables directly, i.e. variables can be only accessed by access functions. class DayOfYear int month; void DayOfYear::set(int new_m, int new_d) month = new_m; day = mew_d; int DayOfYear::get_month() return month; int DayOfYear::get_day() return day; Why private variables? Change of the internal presentation, e.g. variable name, type, will not affect the how the others access the object. Caller still calling the same function with same parameters class DayOfYear void DayOfYear::set(int new_m, int new_d) m = new_m; d = mew_d; int DayOfYear::get_month() int DayOfYear::get_day() Why private members? The common style of class definitions To have all member variables private Provide enough access functions to get and set the member variables Supporting functions used by the member functions should also be made private Only functions that need to interact with the outside can be made public Friend function (optional) In some cases, we want to access the private members of a class through functions outside the class directly Instead of changing the private members into public, we can declare functions with keyword friend A friend function is a normal function defined outside the class, while we declare it inside the class with keyword friend Friend function (optional) class DayOfYear DayOfYear(int m, int d){ month = m; friend void printDate(DayOfYear &d); int month; Friend function (optional) We define and use the friend function like normal function outside the class int main(){ DayOfYear day(1,1); printDate(day); void printDate(DayOfYear &d){ cout<<“The month is “<CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com