我是c ++编程的新手,和其他人一样,我发现复制构造函数的概念有点stange.我经历了一个网站说
复制构造函数是一种特殊的构造函数,它创建一个新对象,它是现有对象的副本,并且有效地执行.
我写了一个代码来创建一个对象,它是另一个对象的副本,发现结果很奇怪,代码如下所示.
#include <iostream>
using namespace std;
class box
{
public:
double getwidth();
box(double );
~box();
box(box &);
private:
double width;
};
box::box(double w)
{
cout<<"\n I'm inside the constructor ";
width=w;
}
box::box(box & a)
{
cout<<"\n Copy constructructor got activated ";
}
box::~box()
{
cout<<"\n I'm inside the desstructor ";
}
double box::getwidth()
{
return width;
}
int main()
{
box box1(10);
box box2(box1);
cout<<"\n calling getwidth from first object : " <<box1.getwidth();
cout<<"\n calling the …Run Code Online (Sandbox Code Playgroud) 我是c ++的初学者,我试图通过创建一个对象并通过点运算符访问它来访问构造函数方法.在这个过程中我遇到了错误.这是正常的吗?我只是想尝试一下.如果有办法做同样的事情请让我知道程序,我用谷歌搜索但找不到任何解决方案.下面是代码.
#include <iostream>
using namespace std;
class box
{
public:
box(double );
private:
double width;
};
box::box(double w)
{
cout<<"\n I'm inside the constructor ";
width=w;
}
box::~box()
{
cout<<"\n I'm inside the desstructor ";
}
int main()
{
box box1;
box1.box(10);
}
Run Code Online (Sandbox Code Playgroud) #include<Stdio.h>
#include<conio.h>
void main()
{
char a[100];
clrscr();
printf("enter a paragraph\n");
scanf("%s",a);
printf("%s",a);
getch();
}
Run Code Online (Sandbox Code Playgroud)
输出:输入一个段落,我的名字是vasanth my
如何使用scanf函数读取整行"我的名字是vasanth"?
struct number {char digits[11];};
Run Code Online (Sandbox Code Playgroud)
以下方法从(*a).digits中删除前导零
void remove_zero(struct number *a);
Run Code Online (Sandbox Code Playgroud)
示例:(*a).digits 000013204 ---> 13204
我的方法是定义一个变量b等于(*a).digits,开始搜索b中的第一个非零数字,然后将(*a).digits替换为b的其余部分.但是,我在实现代码时遇到了麻烦
void remove_zero(struct number *a) {
char b = (*a).digits;
while (b){ // <--- (b) indicates that it hasnt reached the terminator,right?
if (b != '0')
{ //<-- here is where to replace (*a).digits with the rest of b, but how to?
break;
}
}
}
Run Code Online (Sandbox Code Playgroud) 编写了一个例程来交换字符串,第一个例程失败,因为第二个例程正确交换.需要知道相同的原因他们似乎做同样的事情不知道为什么第二个完美的工作不是第一个.
第一种方法
void swap(char *str1, char *str2)
{
char *temp = str1;
str1 = str2;
str2 = temp;
}
Run Code Online (Sandbox Code Playgroud)
第二种方法
void swap(char **str1, char **str2)
{
char *temp = *str1;
*str1 = *str2;
*str2 = temp;
}
Run Code Online (Sandbox Code Playgroud)