1.可变参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <iostream> #include <stdarg.h> // 可变参数的支持 using namespace std;
void sum(int count, ...) { va_list vp;
va_start(vp, count);
int number = va_arg(vp, int); cout << number << endl;
number = va_arg(vp, int); cout << number << endl;
number = va_arg(vp, int); cout << number << endl;
number = va_arg(vp, int); cout << number << endl;
va_end(vp); }
int main() { sum(3, 6,7,8);
return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| void sum(int count, ...) { va_list vp;
va_start(vp, count);
for (int i = 0; i < count; ++i) { int r = va_arg(vp, int); cout << r << endl; }
va_end(vp); }
int main() {
sum(3, 6,7,8);
return 0; }
|
2.static关键字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 错误写法:不能在class中实现 class Dog { public: char * info; int age;
static int id;
Dog() { }
static void update() { }
void update2() { } };
int main() { Dog dog; Dog::update(); return 0; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| 正确写法:
#include <iostream>
using namespace std;
class Dog { public: char * info; int age;
static int id;
static void update() { id += 100;
}
void update2() { id = 13; } };
int Dog::id = 9;
int main() { Dog dog; dog.update2(); Dog::update(); dog.update();
cout << Dog::id << endl; return 0; }
|
3.this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <iostream> // iostream.h 早期C++的方式 using namespace std;
class Student { private: char *name; int age;
public: static int id;
public: void setName(char *name) { this->name = name; } void setAge(int age) { this->age = age; } char *getName() { return this->name; } int getAge() { return this->age; }
public: };
int Student::id = 9527;
int main() { Student student; student.setAge(99); student.setName("Jack"); cout << student.getName() << " , " << student.getAge()<< endl;
Student student1; student1.setAge(88); student1.id = 880;
Student student2; student2.setAge(99); student2.id = 990;
Student::id = 666;
cout << " student1.getAge:" << student1.getAge() << endl;
cout << " student2.getAge:" << student2.getAge() << endl;
cout << "student1.id:" << student1.id << endl; cout << "student2.id:" << student2.id << endl; cout << "Student:::" << Student::id << endl;
return 0; }
Jack , 99 student1.getAge:88 student2.getAge:99 student1.id:666 student2.id:666 Student:::666
|
4.const修饰this
const没有修饰函数,this相当于指针常量,只能修改值,不能修改地址。
如果const修饰了函数,this相当于常量指针常量,只能只读属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| class Worker { public: char * name; int age = NULL;
void change1() {
this->age = 100; this->name = "JJJ"; }
void changeAction() const {
}
void showInfo() const {
cout << "age:" << age << endl; } };
|
5.友元函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <iostream>
using namespace std;
class Person { private: int age = 0;
public: Person(int age) { this->age = age; }
int getAge() { return this->age; }
friend void updateAge(Person * person, int age); };
void updateAge(Person* person, int age) { person->age = age; }
int main() { Person person = Person(9); updateAge(&person, 88);
cout << person.getAge() << endl; return 0; }
|
4.友元类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| using namespace std;
class ImageView { private: int viewSize; friend class Class; };
class Class { public: ImageView imageView;
void changeViewSize(int size) { imageView.viewSize = size; }
int getViewSize() { return imageView.viewSize; } };
int main() { Class mImageViewClass;
mImageViewClass.changeViewSize(600);
cout << mImageViewClass.getViewSize() << endl;
return 0; }
|
5.各种定义和方法的区别
静态函数,友元函数,普通函数,构造函数,析构函数,拷贝构造函数,有什么区别。
头文件定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <iostream> using namespace std;
#ifndef PIG_H #define PIG_H
class Pig { private: int age; char * name;
public: static int id;
Pig(); Pig(char *); Pig(char *,int);
~Pig();
Pig(const Pig & pig);
int getAge(); char * getName(); void setAge(int); void setName(char *);
void showPigInfo() const;
static void changeTag(int age);
friend void changeAge(Pig * pig, int age); };
#endif
|
实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include "Pig.h"
Pig::Pig() { cout << "默认构造函数" << endl; }
Pig::Pig(char * name) { cout << "1个参数构造函数" << endl; }
Pig::Pig(char * name, int age) { cout << "2个参数构造函数" << endl; }
Pig::~Pig() { cout << "析构函数" << endl; }
Pig::Pig(const Pig &pig) { cout << "拷贝构造函数" << endl; }
int Pig::getAge() { return this->age; } char * Pig::getName() { return this->name; } void Pig::setAge(int age) { this->age = age; } void Pig::setName(char * name) { this->name = name; }
void Pig::showPigInfo() const {
}
int Pig::id = 878;
void Pig::changeTag(int age) {
}
void changeAge(Pig * pig, int age) {
}
|
版权声明: 此文章版权归Jack Ou所有,如有转载,请註明来自原作者