//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
using namespace std;
class Student
{
public:
Student()//无参数构造函数
{
number = 1;
score = 100;
}
void show();
PRotected:
int number;
int score;
};
void Student::show()
{
cout<<number<<endl<<score<<endl;
}
void main()
{
Student a;
a.show();
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
using namespace std;
class Teacher
{
public:
Teacher(char *input_name)//有参数的构造函数
{
name=new char[10];
//name=input_name;//这样赋值是错误的
strcpy(name,input_name);
}
void show();
protected:
char *name;
};
void Teacher::show()
{
cout<<name<<endl;
}
void main()
{
//Teacher a;//这里是错误的,因为没有无参数的构造函数
Teacher a("test");
a.show();
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
using namespace std;
class Teacher
{
public:
Teacher(char *input_name)
{
name=new char[10];
//name=input_name;//这样赋值是错误的
strcpy(name,input_name);
}
Teacher()//无参数构造函数,进行函数重载
{
}
void show();
protected:
char *name;
};
void Teacher::show()
{
cout<<name<<endl;
}
void main()
{
Teacher test;
Teacher a("test");
a.show();
cin.get();
}
创建一个无阐述的同名的Teacher()无参数函数,一重载方式区分调用,由于构造函数和普通函数一样具有重载特性所以编写程序的人可以给一个类添加任意多个构造函数,来使用不同的参数来进行初始话对象! 更多内容请看C/C++技术学堂专题,或 现在我们来说一下,一个类对象是另外一类的数据成员的情况,假如有点觉得饶人那么可以简单理解成:类成员的定义可以相互嵌套定义,一个类的成员可以用另一个类进行定义声明。
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
using namespace std;
class Teacher
{
public:
Teacher()
{
Director = new char[10];
strcpy(director,"王大力");
}
char *show();
protected:
char *director;
};
char *Teacher::show()
{
return director;
}
class Student
{
public:
Student()
{
number = 1;
score = 100;
}
void show();
protected:
int number;
int score;
Teacher teacher;//这个类的成员teacher是用Teacher类进行创建并初始化的
};
void Student::show()
{
cout<<teacher.show()<<endl<<number<<endl<<score<<endl;
}
void main()
{
Student a;
a.show();
Student b[5];
for(int i=0; i<sizeof(b)/sizeof(Student); i++)
{
b[i].show();
}
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
Teacher()
{
director = new char[10];
strcpy(director,"王大力");
//director = new string;
// *director="王大力";//string情况赋值
}
~Teacher()
{
cout<<"释放堆区director内存空间1次";
delete[] director;
cin.get();
}
char *show();
protected:
char *director;
//string *director;
};
char *Teacher::show()
{
return director;
}
class Student
{
public:
Student()
{
number = 1;
score = 100;
}
void show();
protected:
int number;
int score;
Teacher teacher;
};
void Student::show()
{
cout<<teacher.show()<<endl<<number<<endl<<score<<endl;
}
void main()
{
Student a;
a.show();
Student b[5];
for(int i=0; i<sizeof(b)/sizeof(Student); i++)
{
b[i].show();
}
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
Teacher(char *temp)
{
director = new char[10];
strcpy(director,temp);
}
~Teacher()
{
cout<<"释放堆区director内存空间1次";
delete[] director;
cin.get();
}
char *show();
protected:
char *director;
};
char *Teacher::show()
{
return director;
}
class Student
{
public:
Student()
{
number = 1;
score = 100;
}
void show();
protected:
int number;
int score;
Teacher teacher("王大力");//错误,一个类的成员假如是另外一个类的对象的话,不能在类中使用带参数的构造函数进行初始化
};
void Student::show()
{
cout<<teacher.show()<<endl<<number<<endl<<score<<endl;
}
void main()
{
Student a;
a.show();
Student b[5];
for(int i=0; i<sizeof(b)/sizeof(Student); i++)
{
b[i].show();
}
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
using namespace std;
class Teacher
{
public:
Teacher(char *temp)
{
director = new char[10];
strcpy(director,temp);
}
~Teacher()
{
cout<<"释放堆区director内存空间1次";
delete[] director;
cin.get();
}
char *show();
protected:
char *director;
};
char *Teacher::show()
{
return director;
}
class Student
{
public:
Student(char *temp):teacher(temp)
{
number = 1;
score = 100;
}
void show();
protected:
int number;
int score;
Teacher teacher;
};
void Student::show()
{
cout<<teacher.show()<<endl<<number<<endl<<score<<endl;
}
void main()
{
Student a("王大力");
a.show();
//Student b[5]("王大力"); //这里这么用是不对的,数组不能够使用带参数的构造函数,以后我们将具体介绍vector类型
// for(int i=0; i<sizeof(b)/sizeof(Student); i++)
//{
// b[i].show();
//}
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
Teacher(char *temp)
{
director = new char[10];
strcpy(director,temp);
}
~Teacher()
{
cout<<"释放堆区director内存空间1次";
delete[] director;
cin.get();
}
char *show();
protected:
char *director;
};
char *Teacher::show()
{
return director;
}
class Student
{
public:
Student(char *temp,int &pk):teacher(temp),pk(pk),ps(10)
{
number = 1;
score = 100;
}
void show();
protected:
int number;
int score;
Teacher teacher;
int &pk;
const int ps;
};
void Student::show()
{
cout<<teacher.show()<<endl<<number<<endl<<score<<endl<<pk<<endl<<ps<<endl;
}
void main()
{
char *t_name="王大力";
int b=99;
Student a(t_name,b);
a.show();
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
#include <string>
using namespace std;
class Test
{
public:
Test(int a)
{
kk=a;
cout<<"构造参数a:"<<a<<endl;
}
public:
int kk;
};
void fun_t(int n)
{
static Test a(n);
//static Test a=n;//这么写也是对的
cout<<"函数传入参数n:"<<n<<endl;
cout<<"对象a的属性kk的值:"<<a.kk<<endl;
}
Test m(100);
void main()
{
fun_t(20);
fun_t(30);
cin.get();
}
//程序作者:管宁
//站点:www.cndev-lab.com
//所有稿件均有版权,如要转载,请务必闻名出处和作者
#include <iostream>
using namespace std;
class Test
{
public:
Test(int j):pb(j),pa(pb+5)
{
}
public:
int pa;
int pb;
};
void main()
{
Test a(10);
cout<<a.pa<<endl;
cout<<a.pb<<endl;
cin.get();
}
新闻热点
疑难解答