C++ Programming Language
Lecture 6&7: Classes
1
Contents
Characteristics of OOP
The definition and declaration of class
Accessing manners
Member of class
Constructor
Copy-constructor
Destructor
bination
Static members
2
OOP Language
Main Viewpoint:
OOP Language can describe the existing things (object) in the nature and their relations directly.
Characteristics:
Belonging to advanced language.
Looking on the impersonal things as objects with some attributes and behaviors.
Describing the objects with same attributes and behaviors as a Class.
Realizing code reuse through inheritance and polymorphism.
3
Example of structure:
struct Date{
int year;
int month;
int day;
};
void print(Date s){
('0');
cout<<setw(4)<<<<'-'<<setw(2)<<<<'-'<<setw(2)<<<<'\n';
(' ');
}
bool isLeapYear(Date d){
return ( % 4==0 && % 100!=0)||( % 400==0);}
4
#include<iostream>
#include<iomanip>
using namespace std;
void main(){
Date d;
= 2000;
= 12;
= 6;
if(isLeapYear(d))
print(d);
}
5
Example of class:
class person{
char Name[20];
int Age;
char Sex;
public:
void Register(char *, int, char);
char *GetName(){return Name;}
int GetAge(){return Age;}
char GetSex(){return Sex;}
void show() {cout<<GetName()<<'\t'<<GetAge()<<'\t'<<
GetSex()<<endl;}
};
6
void person::Register(char * name, int age, char sex)
{
strcpy(Name,name);
Age=age;
Sex=sex;
}
#include<iostream>
#include <string>
using namespace std;
void main()
{
char name[20],sex;
int age;
cin>>name>>age>>sex;
person per1;
(name,age,sex);
();}
7
OOP Language(continue)
Basic concept of OOP
Class
Object
Encapsulation and Concealment
Inheritance and Reuse
Polymorphism
8
Class of OOP
Object collection with the same attributes and behaviors.
Providing abstract description for all objects belongs to the same class, prised attributes and behaviors.
9
Class of OOP
Relation between class and ob
c 课件7 来自淘豆网m.daumloan.com转载请标明出处.