[C + +] class __c++

Source: Internet
Author: User
Tags array length gety shallow copy
instantiating a class
#include <iostream> #include "Circle.h" using namespace std; int main () {//Object two instantiation//first: the stack is instantiated.
	This instance object is used up to automatically destroy the memory.
	Circle C;
	C.SETR (100);
	cout << "C area is:" << c.setarea () << Endl;

	cout << "C circumference is:" << C.setgirth () << Endl;
	Circle cs[2];
		for (int i = 0; i < 2; i++) {cs[i].setr + i);
		cout << "*cs[" << I << "] area is:" << cs[i].setarea () << Endl;
	cout << "*cs[" << I << "] the circumference is:" << Cs[i].setgirth () << Endl;
	cout << "----------------------" << Endl; Second: instantiation of the heap.
	This instance object must be destroyed manually.
	Circle *CP = new Circle ();
	if (NULL = cp) {return 0;
	} cp->setr (100);
	cout << "*CP area is:" << cp->setarea () << Endl;
	cout << "*CP circumference is:" << Cp->setgirth () << Endl;
	Delete cp;
	
	CP = NULL;
	Circle *cps = new Circle[2];
	if (NULL = = cps) {return 0;
		for (int i = 0; i < 2; i++) {cps[i].setr + i); cout << "*cps["<< i <<"] area is: "<< cps[i].setarea () << Endl;"
	cout << "*cps[" << I << "] the circumference is:" << Cps[i].setgirth () << Endl;
	} Delete[]cps;
	CPS = NULL; #if 0 C area is: 31400 C circumference is: 628 *cs[0] area is: 31400 *cs[0] The circumference is: 628 *cs[1] The area is: 32031.1 *cs[1] The circumference is: 634.28----------------- -----*CP area is: 31400 *cp circumference is: 628 *cps[0] The area is: 31400 *cps[0] The circumference is: 628 *cps[1] The area is: 32031.1 *cps[1] The circumference is: 634.28 #endif s
	Ystem ("pause");
return 0; }
encapsulation of classes and outer class definitionsStudent.h
#pragma once
#include <string>

using namespace std;

Class Student
{public
:
	Student ();

	String GetName ();
	void SetName (string _name);
	int getage ();
	void setage (int _age);

	~student ();

Private:
	string m_strname;
	int m_iage;
};


Student.cpp
#include "Student.h"
#include <iostream>
#include <string>

using namespace std;

Student::student ()
{
}

string Student::getname ()
{return
	m_strname;
}
void Student::setname (string _name)
{
	m_strname = _name;
}
int Student::getage ()
{return
	m_iage;
}
void student::setage (int _age)
{
	if (_age < && _age > 0)
	{
		m_iage = _age;
	}
	else {
		m_iage =-1;
	}
}

Student::~student ()
{
}

Main.cpp
int main ()
{
	Student stu;
	Stu.setage ();
	cout << "Student Age is" << stu.getage () << Endl;

#if 0
	Student
#endif

	system ("pause");
	return 0;
}

Memory PartitionsStack area: int x = 0; int* p = NULL; Heap area: int *p = new INT[20]; Global Zone: Stores global variables and static variable constant areas: string str = "Hello world"; Code area: Storing the binary of logical code as shown in figure:


constructor Initialization listStudent.h
Class Student
{public
:
	Student (String name= "Alex", int age=12);
...

Student.cpp
Initialize list feature
//1. Initialization list is performed prior to constructor/
/2. The initialization list can be used only for constructors
//3. Initialization lists simultaneously initialize multiple data member
Student::student ( string name, int age): M_strname (name), M_iage (age) {
	cout << name << "" << age << Endl;
}

Copy ConstructorsFormat: Class name (const class name & variable name) Example:
Copy constructor
Student (const student& stu) {

}
If a copy constructor is not defined, the system automatically generates a copy constructor by default when the system automatically invokes the copy constructor when initializing or replicating the instantiated object
Object MembersExercise: Object member requirements: Define two classes: coordinates class: Coordinate data member: Horizontal axis M_ix, ordinate M_iy member function: constructors, destructors, data encapsulation function segment class: Line data member: Point a M_coora, point B m_coorb member functions: Constructors, destructors, data encapsulation functions
Coordinate.h
#pragma once
class coordinate
{public
:
	coordinate (int x, int y);
	~coordinate ();
	int GetX ();
	void SetX (int x);
	int GetY ();
	void sety (int y);
Private:
	int m_ix;
	int m_iy;
};


Coordinate.cpp
#include "Coordinate.h"
#include <iostream>

using namespace std;


coordinate::coordinate (int x, int y)
{
	m_ix = x;
	M_iy = y;
	cout << "coordinate ()" << m_ix << ", << m_iy << Endl;
}


Coordinate::~coordinate ()
{
	cout << "~coordinate ()" << Endl;
}

int Coordinate::getx ()
{return
	m_ix;
}

void Coordinate::setx (int x)
{
	m_ix = x;
}

int coordinate::gety ()
{return
	m_iy;
}

void coordinate::sety (int y)
{
	m_iy = y;
}

Line.h
#pragma once
#include "Coordinate.h"

class line
{public
: line
	(int x1, int y1, int x2, int y2);
	~line ();
	Coordinate Geta ();
	void SetA (int x, int y);
	coordinate Getb ();
	void Setb (int x, int y);
	void Printinfo ();
Private:
	coordinate M_coora;
	coordinate m_coorb;
};


Line.cpp
#include "Line.h"
#include <iostream>

using namespace std;


Line::line (int x1, int y1, int x2, int y2): M_coora (x1, y1), M_coorb (x2, y2)
{
	cout << "line ()" << Endl;
}


Line::~line ()
{
	cout << "~line ()" << Endl;
}

Coordinate Line::geta ()
{return
	m_coora;
}

void Line::seta (int x, int y)
{
	m_coora.setx (x);
	M_coora.sety (y);
}

Coordinate Line::getb ()
{return
	m_coorb;
}

void Line::setb (int x, int y)
{
	m_coorb.setx (x);
	M_coorb.sety (y);
}

void line::p rintinfo ()
{
	cout << m_coora.getx () << "," << m_coora.gety () << "," <&L T M_coorb.getx () << "," << m_coorb.gety () << Endl;
}

Main.cpp
#include <iostream>
#include "Line.h"

using namespace std;


int main ()
{line
	*line = new Line (1,2,3,4);
	Line->printinfo ();
	Delete line;
	line = NULL;
#if 0
	coordinate () 1, 2
	coordinate () 3, 4 line
	()
	1, 2, 3, 4
	~line ()
	~coordinate (
	) Coordinate ()
#endif

	System ("pause");
	return 0;
}

Shallow CopyArray.h
#pragma once
class array
{public
:
	Array ();
	Array (const array& arr);
	~array ();
	void SetCount (int count);
	int GetCount ();
Private:
	int m_icount;
};


Array.cpp
#include "Array.h"
#include <iostream>

using namespace std;


Shallow copy
Array::array ()
{
	cout << Array () << Endl
}
Array::array (const array& arr)
{
	m_icount = Arr.m_icount;
	cout << "Array () &" << Endl;
}


Array::~array ()
{
	cout << "~array ()" << Endl;
}
void Array::setcount (int count)
{
	m_icount = count;
}
int Array::getcount ()
{return
	m_icount;
}

Main.cpp
#include <iostream>
#include "Array.h"

using namespace std;

int main ()
{
	Array arr1;
	Arr1.setcount (ten);

	Array arr2 (arr1);
	cout << "Arr2.m_icount:" << arr2.getcount () << Endl;

#if 0
	Array ()
	array () &
	arr2.m_icount:10
#endif

	System ("pause");
	return 0;
}

Deep CopyArray.h
#pragma once
class Array
{public
:
	Array (int count);
	Array (const array& arr);
	~array ();
	void SetCount (int count);
	int GetCount ();
	void Printinfo ();
Private:
	int m_icount;
	int *m_paddr;
};


Array.cpp
#include "Array.h"
#include <iostream>

using namespace std;


Deep copy
array::array (int count)
{
	m_icount = count;
	M_PADDR = new Int[m_icount];
	cout << "Array ()" << Endl;
}
Array::array (const array& arr)
{
	m_icount = Arr.m_icount;
	M_PADDR = new Int[m_icount];
	for (int i = 0; i < M_icount i++)
	{
		M_paddr[i] = Arr.m_paddr[i];
	}
	cout << "Array () &" << Endl;
}


Array::~array ()
{
	delete[] m_paddr;
	M_paddr = NULL;
	cout << "~array ()" << Endl;
}
void Array::setcount (int count)
{
	m_icount = count;
}
int Array::getcount ()
{return
	m_icount;
}

void Array::p rintinfo ()
{
	cout << "m_paddr value is:" << m_paddr << Endl;
}

Main.cpp
#include <iostream>
#include "Array.h"

using namespace std;

int main ()
{
	Array arr1 (5);

	Array arr2 (arr1);

	Arr1.printinfo ();
	Arr2.printinfo ();
	the value of the #if 0 array ()
	array () &
	m_paddr is: The value of the 00537520
	m_paddr is: 00537560
#endif

	System (" Pause ");
	return 0;
}

Object PointerPractice: Define the Coordinate class
Data members: M_ix and M_iy
Declaring object pointers and manipulating objects with pointers

Compute two points, horizontal, ordinate, and

Coordinate.h
#pragma once
class coordinate
{public
:
	coordinate ();
	~coordinate ();
	int GetX ();
	void SetX (int x);
	int GetY ();
	void sety (int y);
	void summarize ();
Private:
	int m_ix;
	int m_iy;
};


Coordinate.cpp
#include "Coordinate.h"
#include <iostream>

using namespace std;


Coordinate::coordinate ()
{
	cout << "coordinate ()" << Endl;
}


Coordinate::~coordinate ()
{
	cout << "~coordinate ()" << Endl;
}
int Coordinate::getx ()
{return
	m_ix;
}
void Coordinate::setx (int x)
{
	m_ix = x;
}
int coordinate::gety ()
{return
	m_iy;
}
void coordinate::sety (int y)
{
	m_iy = y;
}
void Coordinate::summarize ()
{
	int sum = M_ix + M_iy;
	cout << "M_ix + m_iy =" << sum << endl;
}

Main.cpp
#include <iostream>

using namespace std;

/*
Requirements:
define coordinate class
data members: M_ix and M_iy
declare object pointers and compute two points through pointer manipulation objects

, horizontal, ordinate and/or
#include " Coordinate.h the
instantiated object in the int main ()
{
	//heap
	coordinate *P1 = new coordinate;//default constructor, there can be no parentheses
	p1-& Gt;setx (10); Pointer Assignment
	(*P1). Sety (2);//Object Assignment method

	coordinate *p2 = NULL;
	P2 = new coordinate ();
	P2->setx (a);
	(*P2). Sety (a);

	cout << "P1. X + P2. X = "<< p1->getx () + p2->getx () << Endl;
	cout << "P2. X + P2. Y = "<< p1->gety () + p2->gety () << Endl;

	Delete P2;
	P2 = NULL;
	Delete P1;
	P1 = NULL;

#if 0
	coordinate ()
	coordinate ()
	P1. X + P2. X =
	P2. X + P2. Y =
	~coordinate ()
	~coordinate ()
#endif

	System ("pause");
	return 0;
}

member Object PointersPractice: Member Object pointers
Define two classes:
Coordinate class: Coordinate
Data member: Horizontal axis m_ix, ordinate M_iy
member functions: Constructors, destructors, data encapsulation functions
Segment class: Line
Data member: Point A pointer m_pcoora, point B pointer m_pcoorb
member functions: constructors, destructors, information printing functions
Coordinate.h
#pragma once
class coordinate
{public
:
	coordinate (int x, int y);
	~coordinate ();
	int GetX ();
	void SetX (int x);
	int GetY ();
	void sety (int y);
Private:
	int m_ix;
	int m_iy;
};

Coordinate.cpp
#include "Coordinate.h"
#include <iostream>

using namespace std;


coordinate::coordinate (int x, int y)
{
	m_ix = x;
	M_iy = y;
	cout << "coordinate ()  " << m_ix << ", << m_iy << Endl;
}


Coordinate::~coordinate ()
{
	cout << "~coordinate ()  " << m_ix << "," << M_iy < < Endl;
}

int Coordinate::getx ()
{return
	m_ix;
}

void Coordinate::setx (int x)
{
	m_ix = x;
}

int coordinate::gety ()
{return
	m_iy;
}

void coordinate::sety (int y)
{
	m_iy = y;
}
Line.h
#pragma once
#include "Coordinate.h"

class line
{public
: line
	(int x1, int y1, int x2, int y2);
	~line ();
	void Printinfo ();
Private:
	coordinate *m_pcoora;
	coordinate *m_pcoorb;
};

Line.cpp
#include "Line.h"
#include <iostream>

using namespace std;


Line::line (int x1, int y1, int x2, int y2)
{
	M_pcoora = new Coordinate (x1, y1);
	M_pcoorb = new Coordinate (x2, y2);
	cout << "line ()" << Endl;
}


Line::~line ()
{
	delete m_pcoora;
	M_pcoora = NULL;
	Delete M_pcoorb;
	M_pcoorb = NULL;
	cout << "~line ()" << Endl;
}


void line::p rintinfo ()
{
	cout << "Printinfo ()" << Endl;
	cout << "(<< m_pcoora->getx () <<", "<< m_pcoora->gety () <<") "<< endl;
  
   cout << "(<< m_pcoorb->getx () <<", "<< m_pcoorb->gety () <<") "<< Endl;
   }
  
Main.cpp
#include <iostream>
#include "Line.h"

using namespace std;

/*
	member object Pointer
	definition two classes:
		coordinate class: Coordinate
		data member: Horizontal axis M_ix, ordinate m_iy
		member function: constructors, destructors, data encapsulation function
		Segment class: Line
		data member: Point A pointer m_pcoora, point B pointer m_pcoorb
		member function: constructor, destructor, information printing function/
int main ()
{line
	*p = new Line (1, 2, 3, 4);
	P->printinfo ();

	Delete p;
	p = NULL;

#if 0
	coordinate ()  1, 2
	coordinate ()  3, 4 line
	()
	printinfo ()
	(1, 2)
	(3, 4)
	~coordinate ()  1, 2
	~coordinate ()  3, 4
	~line ()
#endif

	System ("pause");
	return 0;
}

This pointerExercise: Define an array class
Data member: M_ilen represents array length
member functions:
Constructors
destructor
Len's wrapper function
Information output function Printinfo

Array.h
#pragma once
class Array
{public
:
	Array (int len);
	~array ();
	int Gelen ();
	array* setlen (int len);
	array* printinfo ();
Private:
	int len;


Array.cpp
#include "Array.h"
#include <iostream>

using namespace std;


Array::array (int len)
{
	this->len = len;
	cout << "Array ()" << Endl;
}


Array::~array ()
{
	cout << "~array ()" << Endl;
}

int Array::gelen ()
{return
	this->len;
}
array* array::setlen (int len)
{
	this->len = len;
	return this;
}
array* Array::p rintinfo ()
{
	cout << "Length" << this->len << Endl;
	return this;
}
Main.cpp
#include <iostream>
#include "Array.h"
using namespace std;
	/* Defines an array class
	data member: M_ilen represents an array-length
	member function: function
		destructor
		Len's encapsulated function
		information output function printinfo
* *
int main ()
{
	Array arr1 (a);
	Arr1.printinfo ()->setlen ()->printinfo ();
#if 0
	Array ()
	length is
#endif
	System ("pause");
	return 0;
}

constant pointers and constant referencesPractice: Define a coordinate class, instantiate the coordinate class regular object on the stack, give the coordinates (3,5), then define the constant reference, constant pointer, and finally print the coordinate information by using the object, reference, and pointer to print the function by the call information.

Print the results as follows:
(3,5)
(3,5)
(3,5)
#include <iostream>
using namespace std;
Class coordinate
{public
    
:
	coordinate (int x, int y)
	{
		//set x,y coordinates
		m_ix = x;
        M_iy = y;
	}
    Implement the constant member function
	void Printinfo () const
	{
	    cout << "(" << m_ix << "," << m_iy << ")" <<endl;
	}
Public:
	int m_ix;
	int m_iy;
};


int main (void)
{
	Const coordinate coor (3, 5);

	Create constant pointer p
	Const coordinate *p = &coor;
    Create frequently referenced c
    const coordinate &c = coor;
	
	Coor.printinfo ();
	P->printinfo ();
	C.printinfo ();  
	
	return 0;
}









Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.