– 至少掌握一种面向对象的程序设计语言,如C++
– 深入理解封装、继承和多态等面向对象的重要概念
– 精通一种元语言,如UML,在概念层次上描述设计
– 学习设计模式,源自多年成功经验的积累和总结
访问控制限定符
#include <iostream>
using namespace std;
class Student
{
int m_age;
string m_name;
int m_no;
public:
Student(const string& name,int age,int no)
{
m_name = name;
m_age = age;
m_no = no;
}
void eat(const string& food)
{
cout << "eat " << food << endl;
}
void study(const string& course)
{
cout << "study " << course << endl;
}
void who(void)
{
cout << "name: " << m_name << endl;
cout << "age : " << m_age << endl;
cout << "school number: " << m_no << endl;
}
void setAge(int age)
{
if(age<0)
cout << "age wrong" << endl;
else
m_age = age;
}
void setNo(int no)
{
if(no < 0)
cout << "no wrong" << endl;
else
m_no = no;
}
void setName(const string& name)
{
m_name = name;
}
void foo(const Student& s)
{
cout << s.m_name << endl;
}
};
class Teacher
{
string m_name;
int m_age;
public:
void who(void)
{
cout << m_name << "," << m_age << endl;
}
};
int main()
{
/*
Student s;
s.setAge(25);
s.setName("zhang san");
s.setNo(1001);
s.who();
s.eat("noodle");
s.study("c++");
*/
Student s("zhang san",24,1001);
s.eat("noodel");
s.study("study");
Student s2("wang wu",35,2003);
// s2.setName("wang wu");
s.foo(s2);
Student* ps = new Student("zhao si",34,5654);
ps->who();
ps->eat("ham");
ps->study("Unix");
// -----------------------------
Teacher t1;
t1.who();
return 0;
}
将类的声明、实现与使用分别放在不同的文件里
在栈中创建对象数组
类名 对象数组[元素个数];
类名 对象数组[元素个数] = {类名 (实参表), …};
类名 对象数组[] = {类名 (实参表), …};
在堆中创建/销毁单个对象
类名* 对象指针 = new 类名;
类名* 对象指针 = new 类名 ();
类名* 对象指针 = new 类名 (实参表);
delete 对象指针;
实例:
student.h
// 声明Student类
#ifndef _STU_H
#define _STU_H
#include <string>
using namespace std;
class Student
{
string m_name;
int m_age;
int m_no;
public:
Student(const string& name,int age,int no);
Student();
void who();
void eat(const string& food);
void study(const string& course);
};
#endif // _STU_H
student.cpp
// 实现Student类
#include "02stu.h"
#include <iostream>
using namespace std;
Student::Student(const string& name,int age,int no)
{
m_name = name;
m_age = age;
m_no = no;
}
Student::Student()
{
m_name = "no name";
m_age = 34;
m_no = 234;
}
void Student::who()
{
cout << "name: " << m_name << endl;
cout << "age: " << m_age << endl;
cout << "no : " << m_no << endl;
}
void Student::eat(const string& food)
{
cout << "eat " << food << endl;
}
void Student::study(const string& course)
{
cout << "study " << course << endl;
}
main.cpp
#include "02stu.h"
#include <iostream>
using namespace std;
int main()
{
Student s("zhang san",24,2002);
Student s2;
s.who();
s.study("c++");
s.eat("noodle");
s2.who();
Student sa[5];
sa[0].who();
sa[4].who();
Student sa2[3] = {
Student("zhao si",34,33),
Student("li si",54,24)
};
sa2[0].who();
sa2[1].who();
sa2[2].who();
cout << sizeof(s) << endl;
Student *ps1 = new Student("wang wu",34,3566);
ps1->who();
delete ps1;
ps1 = new Student[5];
ps1[0].who();
ps1[4].who();
delete[] ps1;
ps1 = new Student[3] {
Student("wang yi",34,234),
Student("wang er",35,355),
Student("wang san",36,356)
};
ps1[0].who();
ps1[2].who();
delete[] ps1;
return 0;
}
练习:
实现一个电子时钟类,其构造函数接受当前系统时间,以秒为单位持续运行
#include <iostream>
#include <cstdio>
using namespace std;
class Clock {
public:
Clock (time_t t) {
tm* local = localtime (&t);
m_hour = local->tm_hour;
m_min = local->tm_min;
m_sec = local->tm_sec;
setbuf (stdout, NULL);
}
void run (void) {
for (;;) {
show ();
tick ();
}
}
private:
void show (void) {
printf ("\r%02d:%02d:%02d",
m_hour, m_min, m_sec);
// fflush (stdout);
}
void tick (void) {
sleep (1);
if (++m_sec == 60) {
m_sec = 0;
if (++m_min == 60) {
m_min = 0;
if (++m_hour == 24)
m_hour = 0;
}
}
}
int m_hour, m_min, m_sec;
};
int main (void) {
Clock clock (time (NULL));
clock.run ();
return 0;
}
原文:http://www.cnblogs.com/yanyun888/p/6493113.html