Day3知识点:
1.类的创建实例
例1.C中闰年的判断
//date.h
#ifndef DATE_H
#define DATE_H
struct Date{
int year;
int month;
int day;
};
void init(Date & a);
void print(Date & a);
bool isLeapYear(Date & a);
#endif // DATE_H
//date.cpp
#include "date.h"
#include <iostream>
using namespace std;
void init(Date & a)
{
cin >> a.year >> a.month >> a.day;
}
void print(Date & a)
{
cout<<"year: "<<a.year<<" month: "<<a.month<<" day: "<<a.day<<endl;
}
bool isLeapYear(Date & a)
{
if ((!(a.year % 4) && a.year % 100) || !(a.year % 400))
return true;
else
return false;
}
//main.cpp
#include<iostream>
#include"date.h"
using namespace std;
int main(int argc,int * argv[])
{
Date a;
init(a);
print(a);
// a.year = 1999; //可以在此改变结构体中的值
if (isLeapYear(a))
{
cout << a.year <<" is a leap year! "<< endl;
}
else
{
cout << a.year << " is not a leap year! " << endl;
}
return 0;
}
例2.C++中利用类的成员函数闰年的判断
//date.h
#ifndef DATE_H
#define DATE_H
class Date{
public:
void init();
void print();
bool isLeapYear();
int get_year();
private:
int year;
int month;
int day;
};
#endif // DATE_H
//date.cpp
#include "date.h"
#include <iostream>
using namespace std;
void Date::init()
{
cin >>year >>month >>day;
}
void Date::print()
{
cout<<"year: "<<year<<" month: "<<month<<" day: "<<day<<endl;
}
bool Date::isLeapYear()
{
if ((!(year % 4) && year % 100) || !(year % 400))
return true;
else
return false;
}
int Date::get_year()
{
return year;
}
//main.cpp
#include<iostream>
#include"date.h"
using namespace std;
int main(int argc,int * argv[])
{
Date a;
a.init();
a.print();
if (a.isLeapYear())
{
cout << a.get_year() <<" is a leap year! "<< endl;
}
else
{
cout << a.get_year() << " is not a leap year! " << endl;
}
return 0;
}
2. constructor:构造器:
1) 与类名相同,无返回,被系统生成对象时自动调用,用于初始化。
2) 可以有参数,默认参数,可以重载,重载和默认不能同时使用,包含标配,为了对象的无参创建。
3) 若未提供任何构造,系统默认生成一个无参构造器。
#include <iostream>
#include<string.h>
using namespace std;
class Stu
{
public:
Stu(string na)
:len(strlen(na.c_str())),name(na)
{
}
void dis()
{
cout<<len<<endl;
}
private:
int len;
string name;
};
int main()
{
Stu s("china");
s.dis();
return 0;
}
例3.已定义栈空间(无构造函数)的堆栈弹栈模拟
//stack.h
#ifndef STACK_H
#define STACK_H
class Stack
{
public:
void init();
bool isEmpty();
bool isFull();
char push(char c);
void pop();
private:
char space[1000];
int top = 0;
};
#endif // STACK_H
//stack.cpp
#include "stack.h"
#include<iostream>
#include<string.h>
using namespace std;
void Stack::init()
{
top = 0;
memset(space,0,1024);
}
bool Stack::isEmpty()
{
return top == 0;
}
bool Stack::isFull()
{
return top == 1024;
}
char Stack::push(char c)
{
space[top++] = c;
}
void Stack::pop()
{
cout<<space[--top]<<endl;
}
//main.cpp
#include <iostream>
#include "stack.h"
using namespace std;
int main(int argc, char *argv[])
{
Stack a;
// if(!(a.isFull()))
// a.push('a');
// if(!(a.isFull()))
// a.push('b');
// if(!(a.isFull()))
// a.push('c');
// if(!(a.isFull()))
// a.push('d');
// if(!(a.isFull()))
// a.push('e');
// if(!(a.isFull()))
// a.push('f');
// if(!(a.isFull()))
// a.push('g');
for(char c='a';(c<='z')&&(!a.isFull());c++)
a.push(c);
for(;!(a.isEmpty());)
a.pop();
return 0;
}
例4.自定义栈空间的堆栈弹栈模拟
//stack.h
#ifndef STACK_H
#define STACK_H
class Stack
{
public:
Stack(int size = 1024)
{
top = 0;
space = new char[size];
_size = size;
}
void init();
bool isEmpty();
bool isFull();
char push(char c);
void pop();
private:
char *space;
int top = 0;
int _size;
};
#endif // STACK_H
//stack.cpp
#include "stack.h"
#include<iostream>
#include<string.h>
using namespace std;
void Stack::init()
{
top = 0;
memset(space,0,_size);
}
bool Stack::isEmpty()
{
return top == 0;
}
bool Stack::isFull()
{
return top == _size;
}
char Stack::push(char c)
{
space[top++] = c;
}
void Stack::pop()
{
cout<<space[--top]<<endl;
}
//main.cpp
#include <iostream>
#include "stack.h"
using namespace std;
int main(int argc, char *argv[])
{
Stack a(15);
for(char c='a';(c<='z')&&(!a.isFull());c++)
a.push(c);
for(;!(a.isEmpty());)
a.pop();
return 0;
}
3. destructor:析构器
1) 与类名相同(前面加~,如~Stack(){}),无参(不能重载),无返回。
2) 对象消失的时候自动被调用用于对象销毁之前的内存处理工作。
3) 若未提供,系统默认生成一个空析构器。
4) 在两种情况下被调用:1.栈对象离开作用域;2.堆对象被delete
#include <iostream>
using namespace std;
class Stu
{
public:
Stu()
{
name = new char[100];
}
~Stu()
{
delete name;
}
private:
int a;
char* name;
};
int main(int argc, char *argv[])
{
Stu* s = new Stu;
delete s;
return 0;
}
#include <iostream>
using namespace std;
struct Node
{
char data;
struct Node* next;
};
class List
{
public:
List * createList(); //用于给head初始化
void insertList(char d);
void traverseList();
private:
Node * head;
};
List * List::createList()
{
head = new Node;
head->next = NULL;
}
void List::insertList(char d)
{
Node* cur = new Node;
cur->data = d;
cur->next = head->next; //链表逆序
head->next = cur;
}
void List::traverseList()
{
Node* ph = head->next;
while(ph != NULL)
{
cout<<ph->data<<endl;
ph = ph->next;
}
}
int main(int argc, char *argv[])
{
List list;
list.createList();
for(char i = 'a';i<='z';i++)
list.insertList(i);
list.traverseList();
return 0;
}
例6.带构造析构函数的链表创建
//myList.h
#ifndef MYLIST_H
#define MYLIST_H
struct node
{
int data;
struct node* next;
};
class myList
{
public:
myList();
~myList();
void createList();
void insertList(int d);
void traverseList();
private:
struct node* head;
};
#endif // MYLIST_H
//mylist.cpp
#include "mylist.h"
#include<stdio.h>
#include<iostream>
using namespace std;
myList::myList()
{
head = new node;
head->next = NULL;
}
myList::~myList()
{
node* t = head;
while(head)
{
// t = head->next;
// delete head;
// head = t;
t = head;
head = head->next;
delete t;
}
}
void myList::insertList(int d)
{
node* insert = new node;
insert->data = d;
insert->next = head->next;
head->next = insert;
}
void myList::traverseList()
{
node *ph = head->next;
while(ph != NULL)
{
cout<<ph->data<<endl;
ph = ph->next;
}
}
//main.cpp
#include <iostream>
#include "mylist.h"
using namespace std;
int main()
{
myList list;
list.insertList(1);
list.insertList(2);
list.insertList(3);
list.insertList(4);
list.insertList(6);
list.traverseList();
return 0;
}
4.在构造类时通常情况下无参构造器要包含在内,在提供了默认参数或重载时时可以不包含,其中用默认比较好,默认参数只能用在声明中。
A(){};//可省略
A(int i = 0,int j = 0,int k = 0):x(i),y(j),z(k){};