类的初步学习(关于类的私有与公有成员、类链表、析构函数、友元函数)
1.关于类的私有与公有成员
#include<iostream>
using namespace std;
class Node
{
int data;//私有数据成员可以在该类的公有函数里面调用,但是在main函数下不可调用
public:
void print(int n);
};
void Node::print(int n)
{
data = n;
cout << data;
}
int main()
{
Node s;
int n, a, b;
cin >> n;
s.print(n);
return 0;
}
- 类链表
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
class List
{
public:
Node* head;
void createlist1(Node*&head,int a);//建立逆序链表
void createlist2(Node*&head,int b);//建立正序链表
void showlist(Node*head);//遍历链表
};
void List::createlist1(Node*&head,int a)
{
Node* s = new Node;
Node* p, * q;
s->data = a;
s->next = NULL;
if (head == NULL)
{
head = s;
return;
}
if (head->data < s->data)
{
s->next = head;
head = s;
return;
}
for (q = head, p = head->next; p; q = p, p = p->next)
{
if (p->data < s->data)
{
s->next = p;
q->next = s;
return;
}
}
q->next = s;
return;
}
void List::createlist2(Node*& head, int b)
{
Node* s = new Node;
Node* p, * q;
s->data = b;
s->next = NULL;
if (head == NULL)
{
head = s;
return;
}
if (head->data > s->data)
{
s->next = head;
head = s;
return;
}
for (q = head, p = head->next; p; q = p, p = p->next)
{
if (p->data > s->data)
{
s->next = p;
q->next = s;
return;
}
}
q->next = s;
return;
}
void List::showlist(Node*head)
{
while (head)
{
cout << head->data << ' ';
head = head->next;
}
}
int main()
{
int n, a, b;
while (1)
{
cin >> n;
if (n == 0) break;
if (n == 1)
{
List l1;
l1.head = NULL;
cin >> a;
while (a != 0)
{
l1.createlist1(l1.head, a);
cin >> a;
}
l1.showlist(l1.head);
cout << endl;
}
if (n == 2)
{
List l2;
l2.head = NULL;
cin >> b;
while (b != 0)
{
l2.createlist2(l2.head,b);
cin >> b;
}
l2.showlist(l2.head);
cout << endl;
}
}
return 0;
}
- 析构函数
delete指针和不delete指针的区别
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "As cons." << endl;
}
virtual ~A()
{
cout << "As des." << endl;
}
virtual void f()
{
cout << "As f()." << endl;
}
void g()
{
f();
}
};
class B :public A
{
public:
B()
{
f(); cout << "Bs cons." << endl;
}
~B()
{
cout << "Bs des." << endl;
}
};
class C :public B
{
public:
C()
{
cout << "Cs cons." << endl;
}
~C()
{
cout << "Cs des." << endl;
}
void f()
{
cout << "Cs f()." << endl;
}
};
void main()
{
A* a = new C;
a->g();
/*delete a;*/
}
- 友元函数
#include <iostream>
using namespace std;
class box
{
private:
int color;
int upx, upy;
int lowx, lowy;
public:
friend class line;
void set_color(int c) { color = c; }
void define_box(int x1, int y1, int x2, int y2)
{
upx = x1; upy = y1; lowx = x2; lowy = y2;
}
};
class line
{
private:
int color;
int startx, starty;
int endx, endy;
public:
friend int same_color(line l, box b);
void set_color(int c) { color = c; }
void define_line(int x1,int y1,int x2,int y2)
{
startx = x1; starty = y1; endx = x2; endy = y2;
}
};
int same_color(line l, box b)
{
if (l.color == b.color) return 1;
return 0;
}
不用友元函数就会报错(用的友元类)
正确代码:
#include <iostream>
using namespace std;
class line;
class box
{
private:
int color;
int upx, upy;
int lowx, lowy;
public:
friend int same_color(line l, box b);//补空缺
void set_color(int c) { color = c; }
void define_box(int x1, int y1, int x2, int y2)
{
upx = x1; upy = y1; lowx = x2; lowy = y2;
}
};
class line
{
private:
int color;
int startx, starty;
int endx, endy;
public:
friend int same_color(line l, box b);
void set_color(int c) { color = c; }
void define_line(int x1,int y1,int x2,int y2)
{
startx = x1; starty = y1; endx = x2; endy = y2;
}
};
int same_color(line l, box b)
{
if (l.color == b.color) return 1;
return 0;
}