8.7 c++

指针

指针是一个变量,它存储另一个变量的内存地址。
使用*符号声明指针。
使用&符号获取变量地址。

每一个变量都有属于他的内存地址

#include <iostream>
 
using namespace std;
 
int main ()
{
   int  var1;
   char var2[10];
 
   cout << "var1 变量的地址: ";
   cout << &var1 << endl;
 
   cout << "var2 变量的地址: ";
   cout << &var2 << endl;
 
   return 0;
}
#include <iostream>

int main() {
    int a = 10;
    int *ptr = &a; // 声明指针并初始化

    std::cout << "Value of a: " << a << std::endl;
    std::cout << "Address of a: " << &a << std::endl;
    std::cout << "Value of ptr: " << ptr << std::endl;
    std::cout << "Value pointed to by ptr: " << *ptr << std::endl;

    return 0;
}

引用

引用是一个别名,它引用另一个变量。
使用&符号声明引用。

#include <iostream>

int main() {
    int a = 10;
    int &ref = a; // 声明引用

    std::cout << "Value of a: " << a << std::endl;
    std::cout << "Value of ref: " << ref << std::endl;

    ref = 20; // 通过引用修改a的值

    std::cout << "Value of a after modification: " << a << std::endl;
    std::cout << "Value of ref after modification: " << ref << std::endl;

    return 0;
}

结构(struct)

结构体是一种用户自定义的数据类型,用于将不同类型的数据组合在一起。

#include <iostream>

struct Person {
    std::string name;
    int age;
    float height;
};

int main() {
    Person person1;
    person1.name = "Alice";
    person1.age = 25;
    person1.height = 5.7;

    std::cout << "Name: " << person1.name << std::endl;
    std::cout << "Age: " << person1.age << std::endl;
    std::cout << "Height: " << person1.height << std::endl;

    return 0;
}

基本操作

  1. 初始化结构体
    通过列表初始化或使用构造函数初始化。
  2. 访问和修改结构体成员
    使用.运算符访问和修改结构体成员。
#include <iostream>

struct Person {
    std::string name;
    int age;
    float height;
};

int main() {
    // 列表初始化
    Person person2 = {"Bob", 30, 5.9};

    std::cout << "Name: " << person2.name << std::endl;
    std::cout << "Age: " << person2.age << std::endl;
    std::cout << "Height: " << person2.height << std::endl;

    // 修改结构体成员
    person2.age = 31;
    std::cout << "Updated Age: " << person2.age << std::endl;

    return 0;
}
#include <iostream>
#include <cstring>
 
using namespace std;
 
// 声明一个结构体类型 Books 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        // 定义结构体类型 Books 的变量 Book1
   Books Book2;        // 定义结构体类型 Books 的变量 Book2
 
   // Book1 详述
   strcpy( Book1.title, "C++ 教程");
   strcpy( Book1.author, "Runoob"); 
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 12345;
 
   // Book2 详述
   strcpy( Book2.title, "CSS 教程");
   strcpy( Book2.author, "Runoob");
   strcpy( Book2.subject, "前端技术");
   Book2.book_id = 12346;
 
   // 输出 Book1 信息
   cout << "第一本书标题 : " << Book1.title <<endl;
   cout << "第一本书作者 : " << Book1.author <<endl;
   cout << "第一本书类目 : " << Book1.subject <<endl;
   cout << "第一本书 ID : " << Book1.book_id <<endl;
 
   // 输出 Book2 信息
   cout << "第二本书标题 : " << Book2.title <<endl;
   cout << "第二本书作者 : " << Book2.author <<endl;
   cout << "第二本书类目 : " << Book2.subject <<endl;
   cout << "第二本书 ID : " << Book2.book_id <<endl;
 
   return 0;
}

结构体与函数

  1. 将结构体作为函数参数
    传值方式和传引用方式。
  2. 返回结构体
    函数可以返回结构体类型的数据。
#include <iostream>
#include <cstring>
 
using namespace std;
void printBook( struct Books book );
 
// 声明一个结构体类型 Books 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        // 定义结构体类型 Books 的变量 Book1
   Books Book2;        // 定义结构体类型 Books 的变量 Book2
 
    // Book1 详述
   strcpy( Book1.title, "C++ 教程");
   strcpy( Book1.author, "Runoob"); 
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 12345;
 
   // Book2 详述
   strcpy( Book2.title, "CSS 教程");
   strcpy( Book2.author, "Runoob");
   strcpy( Book2.subject, "前端技术");
   Book2.book_id = 12346;
 
   // 输出 Book1 信息
   printBook( Book1 );
 
   // 输出 Book2 信息
   printBook( Book2 );
 
   return 0;
}
void printBook( struct Books book )
{
   cout << "书标题 : " << book.title <<endl;
   cout << "书作者 : " << book.author <<endl;
   cout << "书类目 : " << book.subject <<endl;
   cout << "书 ID : " << book.book_id <<endl;
}

类和对象(Classes and Objects)

类是一个模板,它定义了一组属性(成员变量)和行为(成员函数)。使用class关键字定义类。
对象是类的实例,具有类定义的属性和行为。使用类名创建对象。

#include <iostream>

class Person {
public:
    std::string name;
    int age;

    void display() {
        std::cout << "Name: " << name << std::endl;
        std::cout << "Age: " << age << std::endl;
    }
};

int main() {
    Person person1;
    person1.name = "Alice";
    person1.age = 25;

    person1.display();

    return 0;
}

定义一个类表示学生信息(姓名、年龄、成绩),并创建多个学生对象,输出学生信息。
构造函数:用于初始化对象,在创建对象时自动调用。

#include <iostream>
#include <string>

class Student {
public:
    std::string name;
    int age;
    float grade;

    // 构造函数
    Student(std::string n, int a, float g) {
        name = n;
        age = a;
        grade = g;
    }

    // 成员函数,输出学生信息
    void display() {
        std::cout << "Name: " << name << std::endl;
        std::cout << "Age: " << age << std::endl;
        std::cout << "Grade: " << grade << std::endl;
    }
};

int main() {
    // 创建学生对象
    Student student1("Alice", 20, 85.5);
    Student student2("Bob", 21, 90.0);
    Student student3("Charlie", 19, 88.0);

    // 输出学生信息
    student1.display();
    std::cout << std::endl;
    student2.display();
    std::cout << std::endl;
    student3.display();

    return 0;
}

继承

当创建一个类时,您不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可。这个已有的类称为基类,新建的类称为派生类

// 基类
class Animal {
    // eat() 函数
    // sleep() 函数
};


//派生类
class Dog : public Animal {
    // bark() 函数
};
#include <iostream>
 
using namespace std;
 
// 基类
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};
 
// 派生类
class Rectangle: public Shape
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};
 
int main(void)
{
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);
 
   // 输出对象的面积
   cout << "Total area: " << Rect.getArea() << endl;
 
   return 0;
}

public:
成员可以被任何代码访问,包括类的外部代码。
protected:
成员可以被类的成员函数和派生类的成员函数访问,但不能被类外部的代码直接访问。
private:
成员只能在定义它的类内访问,不能被派生类或类的外部代码访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值