7月24日 学习笔记
//對象初始化列表 對類內調用類(構造函數有參) 在后面直接賦值
#include <iostream>
#include <windows.h>
#pragma warning(disable:4996)
using namespace std;
class Birthday
{
private:
?? ?int year;
?? ?int month;
?? ?int day;
public:
?? ?Birthday(int y,int m ,int d);
?? ?~Birthday();
?? ?void print();
};
Birthday::Birthday(int y, int m, int d)
{
?? ?cout << " birthday constuctor" << endl;
?? ?year = y;
?? ?month = m;
?? ?day = d;
}
Birthday::~Birthday()
{
?? ?cout << "destuctor birthday " << endl;
}
void Birthday::print()
{
?? ?cout << year << "年 " << month << "月 " << day << "日 " << endl;
}
class Student
{
private:
?? ?Birthday m_birth;
?? ?char name[10];
?? ?const int age;
public:
?? ?Student(char *n, int y, int m, int d);
?? ?//把參數加入我們 后面的 ‘類構造函數里’,最前面 ‘類構造函數’ 使用里面的最后幾個值
?? ?~Student();
?? ?void print();
};
Student::Student(char *n, int y, int m, int d) :m_birth(1000, 12, 3), age(10)
//賦值過去用 “:” + “創建的類對象名” ? 對于本身的常變量 也這樣賦值
{
?? ?cout << " student constuctor" << endl;
?? ?strcpy(name,n);
}
Student::~Student()
{
?? ?cout << " student destuctor" << endl;
}
void Student::print()
{
?? ?cout << " ? ?名字:" << name << endl;
?? ?cout << " ? ?年齡:" << age << endl;
?? ?m_birth.print();
}
int main()
{
?? ?Student s1("aaa",1000,12,3);//初始化列表就是 把調用的 另一個類的參數 放在我們創建的類參數里
?? ?s1.print();
?? ?system("Pause");
?? ?return 0;
}
/***********************************************************************************************************************************************/
/*
//手動調用構造函數 匿名對象 在這行代碼結束 就析構了
//但是匿名對象創建了可以復制給新對象 并不等價于 拷貝構造函數
#include <iostream>
#include <windows.h>
using namespace std;
class Test
{
private:
?? ?int m_a;
?? ?int m_b;
public:
?? ?void setab(int _a, int _b);
?? ?Test(int a,int b);
?? ?~Test();
};
void Test::setab(int _a, int _b)
{
?? ?m_a = _a;
?? ?m_b = _b;
}
Test::Test(int a,int b)
{
?? ?cout << "test constuctor " << endl;
}
Test::~Test()
{
?? ?cout << "~test destuct " << endl;
}
int main()
{
?? ?Test t1(1,2);
?? ?Test(1,2);
?? ?Test t3 = Test(2,3);//第一個和第三個是等價的
?? ?system("pause");
?? ?return 0;
}
*/
/***********************************************************************************************************************************************/
#include <iostream>
#include <windows.h>
using namespace std;
class Test
{
private:
?? ?int m_a;
?? ?int m_b;
public:
?? ?void setab(int _a, int _b);
?? ?Test(int a, int b);
?? ?Test(int a);
?? ?~Test();
};
void Test::setab(int _a, int _b)
{
?? ?m_a = _a;
?? ?m_b = _b;
}
Test::Test(int a, int b)
{
?? ?cout << "test constuctor2 " << endl;
}
Test::Test(int a)
{
?? ?cout << "test constuctor1 " << endl;
?? ?Test(1,2);//單個參數的構造函數 里創建匿名對象 不會改變此對象中 賦值過的變量值
//因為 這個匿名對象 在遇到自己的分號的時候就結束了
}
Test::~Test()
{
?? ?cout << "~test destuct " << endl;
}
int main()
{
?? ?Test t1(1, 2);
?? ?Test(1, 2);
?? ?Test t3 = Test(2, 3);//第一個和第三個是等價的
?? ?Test t4(1);
?? ?system("pause");
?? ?return 0;
}
/***********************************************************************************************************************************************/
//有關 new delete
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
?? ?//以前用的malloc
?? ?/*
?? ?int *p1 = (int *)malloc( sizeof( int ) );
?? ?*p1 = 1;
?? ?free( p1 );
?? ?int *p2 = (int *)malloc(sizeof(int) * 10);
?? ?for (int i = 0; i < 10; i++)
?? ?{
?? ??? ?*(p2 + i) = i;
?? ?}
?? ?free(p2);
?? ?*/
?? ?int *p3 = new int(10);//int *p3 = new int(100);
?? ?cout << "p3 = " << *p3 << endl;
?? ?delete p3;
?? ?int *p4 = new int[10];
?? ?cout << "p4 = " << endl;
?? ?for (int i = 0; i < 10; i++)
?? ?{
?? ??? ?cout << ? << endl;
?? ?}
?? ?delete p4;
?? ?system("pause");
?? ?return 0;
}
?
?
/***********************************************************************************************************************************************/
//堆上申請 new/malloc
//調用delete 自動調用析構函數
#include <iostream>
#include <windows.h>
using namespace std;
class Test
{
private:
?? ?int m_a;
public:
?? ?void print();
?? ?Test();
?? ?Test(int x);
?? ?~Test();
};
Test::Test()
{
?? ?cout << "無參構造函數" << endl;
}
Test::Test(int x)
{
?? ?m_a = x;
?? ?cout << "有參構造函數" << endl;
}
Test::~Test()
{
?? ?cout << "~析構函數" << endl;
}
void Test::print()
{
?? ?cout << "a = " << m_a << endl;
}
int main()
{
?? ?Test *t1 = (Test *)malloc(sizeof(Test));
?? ?t1->print();
?? ?free(t1);//用 malloc 不會調用構造函數的 只是分配空間 ?最后需要釋放空間
?? ?Test *t2 = new Test(1);
?? ?t2->print();
?? ?delete t2;//new 與delete 對應
?? ?system("pause");
?? ?return 0;
}
//7-24-下午-自己實現tsl
/**********************************************************************************************************************************************/
main.cpp
#include <iostream>
#include <windows.h>
#include "mylist.h"
using namespace std;
int main()
{
?? ?Mylist mylist;
?? ?Student *s1 = new Student("aaa", 20 ,'m');
?? ?Student *s2 = new Student("bbb", 20, 'm');
?? ?Student *s3 = new Student("ccc", 20, 'm');
?? ?Student *s4 = new Student("ddd", 20, 'm');
?? ?Student *s5 = new Student("eee", 20, 'm');
?? ?Student *s6 = new Student("fff", 20, 'm');
?? ?mylist.push_back(s1);
?? ?mylist.push_back(s2);
?? ?mylist.push_back(s3);
?? ?mylist.push_back(s4);
?? ?mylist.push_back(s5);
?? ?mylist.push_back(s6);
?? ?mylist.list_traval();
?? ?mylist.list_delete(s2);
?? ?mylist.list_traval();
?? ?system("pause");
?? ?return 0;
}
mylist.cpp
#include <iostream>
#include "mylist.h"
#include <windows.h>
using namespace std;
Mylist::Mylist()
{
?? ?head = new Student;
}
Mylist::~Mylist()
{
?? ?cout << "destuct" << endl;
}
void Mylist::push_back(Student *s)
{
?? ?Student *s1 = head;
?? ?while (s1->getnext() != NULL)
?? ?{
?? ??? ?s1 = s1->getnext();
?? ?}?? ?
?? ?s1->setnext(s);
}
void Mylist::list_delete(Student *s)
{
?? ?Student *s2 = head->getnext();
?? ?while (s2 != NULL)
?? ?{
?? ??? ?if (s2->getnext() == s)
?? ??? ?{
?? ??? ??? ?Student *s3 = s2;
? ? ? ? ? ? s2 = s2->next;
? ? ? ? ? ? s3->setnext( s2->getnext() );
?? ??? ??? ?break;
?? ??? ?}?? ?
?? ??? ?else
?? ??? ??? ?s2 = s2->getnext();
?? ?}
}
void Mylist::list_traval()
{
?? ?Student *s2 = head->getnext();
?? ?while (s2 != NULL)
?? ?{
?? ??? ?s2->printall();
?? ??? ?s2 = s2->getnext();
?? ?}
?? ?cout << endl;
}
student.cpp
#include <string.h>
#include "student.h"
#include <iostream>
#pragma warning(disable:4996)
using namespace std;
Student::Student(char *name, int age, char sex)
{
?? ?strcpy(m_name, name);
?? ?m_age = age;
?? ?m_sex = sex;
?? ?next = NULL;
}
int Student::getage()
{
?? ?return m_age;
}
char* Student::getname()
{
?? ?char *name1;
?? ?strcpy(name1, m_name);
?? ?return name1;
}
char Student::getsex()
{
?? ?return m_sex;
}
void Student::printall()
{
?? ?cout << "name = " << m_name << " ?age = " << m_age << " ?sex = " << m_sex << endl;
}
void Student::setnext(Student *s)
{
?? ?next = s;
}
Student * Student::getnext()
{
?? ?return next;
}
?
mylist.h
#ifndef _MYLIST_H_
#define _MYLIST_H_
#include "student.h"
class Mylist
{
private:
?? ?Student *head;
public:
?? ?Mylist();
?? ?~Mylist();
?? ?void push_back(Student *s);
?? ?void list_delete(Student *s);
?? ?void list_traval();
};
#endif
student.h
#ifndef _STUDENT_H_
#define _STUDENT_H_
class Student
{
private:
?? ?int m_age;
?? ?char m_name[10];
?? ?char m_sex;
?? ?Student *next;
public:
?? ?Student(char *name = "xxx", int age = 20, char sex = 'm');
?? ?~Student();
?? ?void setnext(Student *s);
?? ?Student * getnext();
?? ?void printall();
?? ?int getage();
?? ?char* getname();
?? ?char getsex();
};
#endif
?
總結
以上是生活随笔為你收集整理的7月24日 学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7-20牛客网未知点集合
- 下一篇: 7-24练习 测试