c++ 指针 . -< 用法区别
时间: 2024-04-14 17:27:06 浏览: 18
C++中,指针和成员访问运算符(->)有着不同的用法和区别。
指针是一个存储内存地址的变量,可以用来间接访问和操作内存中的数据。通过指针,我们可以在程序中动态地管理和操作内存。指针的声明和使用如下所示:
```cpp
int* ptr; // 声明一个指向整数的指针
int num = 10;
ptr = # // 将指针指向变量num的地址
```
在上述例子中,通过`&`运算符获取了变量`num`的地址,并将该地址赋给指针`ptr`。这样,`ptr`指向了`num`变量。
而成员访问运算符(->)则用于通过指针访问对象或结构体的成员。它提供了一种简洁的方式来访问通过指针间接引用的对象或结构体中的成员。使用成员访问运算符的示例代码如下:
```cpp
struct Person {
string name;
int age;
};
Person* personPtr = new Person;
personPtr->name = "John"; // 通过指针访问结构体成员
personPtr->age = 25;
```
在上述例子中,我们声明了一个指向结构体`Person`的指针`personPtr`,然后使用成员访问运算符(->)来访问并修改结构体成员。
因此,指针用于间接访问和操作内存中的数据,而成员访问运算符(->)用于通过指针访问对象或结构体的成员。
相关问题
每个TwoDimensionShape包含getArea函数,计算二维图形的面积。每个ThreeDimensionShape包含getArea和getVolume函数,计算三维图形的表面积和体积。在主函数中,定义shape的指针数组,分别指向层次结构中的各个具体对象。输出每个对象。在循环中,处理所有图形,确定每个图形是TwoDimensionShape还是ThreeDimensionShape,如果图形是TwoDimensionShape,则显示它的面积。如果图形是ThreeDimensionShape,则显示三维图形的表面积和体积。 程序说明: 1、dynamic_cast强制类型转换 dynamic_cast为C++的强制类型转换方法,主要用于基类和派生类直接的类型转换。更多细节请自行google。 2、输出格式: cout << "Square with side length " <<...<< "; center at (" <<...<< ", " <<... << ")"; cout << "Circle with radius " << ... << "; center at (" << ... << ", " << ...<< ")"; cout << "Cube with side length " <<... << "; center at (" << ... << ", " << ... << ")"; cout << "Sphere with radius " << ... << "; center at (" << ... << ", " << ...<< ")";写个代码
以下是题解:
```cpp
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Shape {
public:
virtual ~Shape() {}
};
class TwoDimensionShape : public Shape {
public:
virtual double getArea() const = 0;
};
class ThreeDimensionShape : public Shape {
public:
virtual double getArea() const = 0;
virtual double getVolume() const = 0;
};
class Point {
public:
Point(double x = 0, double y = 0, double z = 0) : _x(x), _y(y), _z(z) {}
double getX() const { return _x; }
double getY() const { return _y; }
double getZ() const { return _z; }
private:
double _x, _y, _z;
};
class Square : public TwoDimensionShape {
public:
Square(double side, double x, double y) : _side(side), _center(x, y) {}
double getArea() const { return _side * _side; }
Point getCenter() const { return _center; }
private:
double _side;
Point _center;
};
class Circle : public TwoDimensionShape {
public:
Circle(double radius, double x, double y) : _radius(radius), _center(x, y) {}
double getArea() const { return M_PI * _radius * _radius; }
Point getCenter() const { return _center; }
private:
double _radius;
Point _center;
};
class Cube : public ThreeDimensionShape {
public:
Cube(double side, double x, double y, double z) : _side(side), _center(x, y, z) {}
double getArea() const { return 6 * _side * _side; }
double getVolume() const { return _side * _side * _side; }
Point getCenter() const { return _center; }
private:
double _side;
Point _center;
};
class Sphere : public ThreeDimensionShape {
public:
Sphere(double radius, double x, double y, double z) : _radius(radius), _center(x, y, z) {}
double getArea() const { return 4 * M_PI * _radius * _radius; }
double getVolume() const { return 4.0 / 3.0 * M_PI * _radius * _radius * _radius; }
Point getCenter() const { return _center; }
private:
double _radius;
Point _center;
};
int main() {
Shape* shapes[] = {
new Square(2.0, 0.0, 0.0),
new Circle(1.5, 1.0, 1.0),
new Cube(3.0, 2.0, 2.0, 2.0),
new Sphere(2.5, 3.0, 3.0, 3.0)
};
int n = sizeof(shapes) / sizeof(shapes[0]);
for (int i = 0; i < n; i++) {
if (auto p = dynamic_cast<TwoDimensionShape*>(shapes[i])) {
cout << "Square with side length " << sqrt(p->getArea())
<< "; center at (" << p->getCenter().getX() << ", " << p->getCenter().getY() << ")" << endl;
} else if (auto p = dynamic_cast<ThreeDimensionShape*>(shapes[i])) {
cout << "Cube with side length " << pow(p->getArea() / 6, 1.0 / 3.0)
<< "; center at (" << p->getCenter().getX() << ", " << p->getCenter().getY() << ", " << p->getCenter().getZ() << ")" << endl;
cout << "Sphere with radius " << pow(p->getVolume() * 3 / (4 * M_PI), 1.0 / 3.0)
<< "; center at (" << p->getCenter().getX() << ", " << p->getCenter().getY() << ", " << p->getCenter().getZ() << ")" << endl;
}
}
for (int i = 0; i < n; i++) {
delete shapes[i];
}
return 0;
}
```
思路:
本题要求定义两个抽象类 TwoDimensionShape 和 ThreeDimensionShape,分别表示二维图形和三维图形,并在它们的基础上定义 Square、Circle、Cube 和 Sphere 四个具体类。
在主函数中,定义一个指针数组 shapes,分别指向层次结构中的各个具体对象。在循环中,处理所有图形,确定每个图形是 TwoDimensionShape 还是 ThreeDimensionShape,如果图形是 TwoDimensionShape,则显示它的面积。如果图形是 ThreeDimensionShape,则显示三维图形的表面积和体积。
细节:
- 在 Square 和 Circle 中,使用 Point 类保存图形的中心坐标。
- 在 Cube 和 Sphere 中,也使用 Point 类保存图形的中心坐标。
- 在判断图形类型时,使用 dynamic_cast 进行类型转换,如果转换成功,则说明该图形是 TwoDimensionShape 或 ThreeDimensionShape 子类,然后调用相应的函数进行输出。
- 在输出 Cube 和 Sphere 时,分别使用公式计算它们的边长和半径。
时间复杂度:$O(n)$(其中 $n$ 为图形数)
空间复杂度:$O(n)$
完整代码:
#include<iostream> using namespace std; class student { public: //友元函数可以访问类中的公有和私有成员,不可以访问保护成员 friend void func2(student& s); friend class teacher; int a = 10; student(int x); ~student(); void func3(); private: int b = 20; }; student::student(int x) :a(x) { cout << "student 构造函数调用1" << endl; } student::~student() { cout << "student 析构函数调用!" << endl; } void student::func3() { cout << b << endl; } class teacher { public: int a = 1; student p; teacher(int a); ~teacher(); }; teacher::teacher(int a) { cout << "teacher构造函数调用" << endl; this->a = a; // p = new student(100); } teacher::~teacher() { cout << "teacher析构函数调用" << endl; // delete p; } void func1() { student s1(100); cout << s1.a << endl; s1.func3(); func2(s1); cout << s1.a << endl; } //友元函数,全局函数做友元 //在形参中加入const防止传入的参数被改变,不加const则可以改变 void func2( student &s) { //传入普通类对象不可以改变类中的值,只能改变形参类的值 /*s.a = 123; cout<<s.a << endl; cout << s.b << endl;*/ ////传入指针可以改变类的值 /*s->a = 1000; s->b = 123; cout << s->a << endl; cout << s->b << endl;*/ ///传入引用也可以改变类中的值 //s.a = 12345; //s.b = 123456; cout << s.a << endl; cout << s.b << endl; } //类做友元 void func3() { student s1(10); cout << s1.a << endl; teacher t1(100); cout << t1.a << endl; //t1.p->a = 123456; cout << t1.p.a<< endl; } int main() { //成员函数做友元 //func1(); //类做友元 func3(); system("pause"); return 0; }
这是一个 C++程序,它包含了一个名为student的类、一个名为teacher的类和三个全局函数func1、func2和func3。其中,student类中定义了一个友元函数func2和一个私有成员变量b,teacher类中定义了一个student类型的成员变量p和一个构造函数和析构函数,func1函数中创建了一个student对象s1并调用了它的成员函数func3和友元函数func2,func2函数中以传引用的方式接收一个student对象并输出它的公有成员变量a和私有成员变量b的值,func3函数中创建了一个student对象s1和一个teacher对象t1并输出它们的公有成员变量a的值。整个程序的功能是展示了C++中友元函数、友元类和成员函数做友元的用法。