C++ Tutorial/Class/instance object

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Accessing members of objects on the heap

<source lang="cpp">#include <iostream>

class MyClass
{
public:
    MyClass() {
        itsAge = 2; 
    }
    ~MyClass() {}
    int GetAge() const { 
        return itsAge; 
    }
    void SetAge(int age) { 
        itsAge = age; 
    }
private:
    int itsAge;
};

int main()
{
    MyClass * myObject = new MyClass;
    std::cout << "myObject is " << myObject->GetAge() << " years old\n";

    myObject->SetAge(5);
    std::cout << "myObject is " << myObject->GetAge() << " years old\n";

    delete myObject;
    return 0;
}</source>
myObject is 2 years old
myObject is 5 years old

Assign class object

<source lang="cpp">#include <iostream> using std::cout; using std::endl; class Box {

 public:
   double length;
   double width;
   double height;
   double volume() {
     return length * width * height;
   }

}; int main() {

 Box firstBox = { 80.0, 50.0, 40.0 };
 Box secondBox = firstBox;
 secondBox.length *= 1.1;
 secondBox.width *= 1.1;
 secondBox.height *= 1.1;
 cout << secondBox.length
      << secondBox.width
      << secondBox.height
      << endl;
      
 cout << "Volume of second Box object is " << secondBox.volume()
      << endl;
 return 0;

}</source>

885544
Volume of second Box object is 212960

Automatically converted into MyClass(4): MyClass ob = 4;

<source lang="cpp">#include <iostream> using namespace std; class MyClass {

 int a;

public:

 MyClass(int x) { a = x; }
 int geta() { return a; }

}; int main() {

 MyClass ob = 4; // automatically converted into MyClass(4)
 cout << ob.geta();
 return 0;

}</source>

4

Call class constructor without new operator

<source lang="cpp">#include <iostream> using std::cout; using std::endl; class Box {

 public:
   double length;
   double width;
   double height;
   // Constructor
   Box(double lengthValue, double widthValue, double heightValue) {
     cout << "Box constructor called" << endl;
     length = lengthValue;
     width = widthValue;
     height = heightValue;
   }
   double volume() {
     return length * width * height;
   }

}; int main() {

 Box firstBox(80.0, 50.0, 40.0);
 cout << "Size of first Box object is "
      << firstBox.length  << " by "
      << firstBox.width << " by "
      << firstBox.height << "\n "
      << "Volume of first Box object is " << firstBox.volume()
      << endl;
 return 0;

}</source>

Box constructor called
Size of first Box object is 80 by 50 by 40
 Volume of first Box object is 160000

Creates two objects.

<source lang="cpp">#include <iostream> using namespace std;

class ThreeDimension { public:

 int X;
 int Y;   
 int Z;       

};

int main() {

 ThreeDimension aDimension; 
 ThreeDimension bDimension; 

 int range1, range2;  

 aDimension.X = 7; 
 aDimension.Y = 16; 
 aDimension.Z = 21; 
 
 bDimension.X = 2; 
 bDimension.Y = 14; 
 bDimension.Z = 12; 
 range1 = aDimension.Y * aDimension.Z; 
 range2 = bDimension.Y * bDimension.Z; 
 
 cout << range1 << "\n";  

 cout << range2;  
 
 return 0; 

}</source>

336
168

Creating objects on the heap using new

<source lang="cpp">#include <iostream> class MyClass { public:

   MyClass();
   ~MyClass();

private:

   int itsAge;

}; MyClass::MyClass() {

   std::cout << "Constructor called.\n";
   itsAge = 1;

} MyClass::~MyClass() {

   std::cout << "Destructor called.\n";

} int main() {

   std::cout << "MyClass ...\n";
   MyClass Frisky;
   std::cout << "MyClass *pRags = new MyClass...\n";
   MyClass * pRags = new MyClass;
   std::cout << "delete pRags...\n";
   delete pRags;
   std::cout << "Exiting, watch go...\n";
   return 0;

}</source>

MyClass ...
Constructor called.
MyClass *pRags = new MyClass...
Constructor called.
delete pRags...
Destructor called.
Exiting, watch go...
Destructor called.

MyClass ob(4)

<source lang="cpp">#include <iostream> using namespace std; class MyClass {

 int a;

public:

 MyClass(int x) { a = x; }
 int geta() { return a; }

}; int main() {

 MyClass ob(4);
 cout << ob.geta();
 return 0;

}</source>

4

MyClass ob = MyClass(4);

<source lang="cpp">#include <iostream> using namespace std; class MyClass {

 int a;

public:

 MyClass(int x) { a = x; }
 int geta() { return a; }

}; int main() {

 MyClass ob = MyClass(4);
 cout << ob.geta();
 return 0;

}</source>

4

Object assignment

<source lang="cpp">#include <iostream> using namespace std;

class MyClass {

 int a, b; 

public:

 void setAB(int i, int j) { a = i, b = j; } 
 void display() { 
   cout << "\n a is " << a << "\n"; 
   cout << "\n b is " << b << "\n"; 
 } 

};

int main() {

 MyClass ob1, ob2; 

 ob1.setAB(10, 20); 
 ob2.setAB(0, 0); 
 cout << "ob1 before assignment:"; 
 ob1.display(); 
 cout << "ob2 before assignment:"; 
 ob2.display(); 

 ob2 = ob1; // assign ob1 to ob2 

 cout << "ob1 after assignment:"; 
 ob1.display(); 
 cout << "ob2 after assignment:"; 
 ob2.display(); 

 ob1.setAB(-1, -1); // change ob1 

 cout << "ob1 after changing ob1:"; 
 ob1.display(); 
 cout << "ob2 after changing ob1:"; 
 ob2.display(); 

 return 0; 

}</source>

ob1 before assignment:
 a is 10
 b is 20
ob2 before assignment:
 a is 0
 b is 0
ob1 after assignment:
 a is 10
 b is 20
ob2 after assignment:
 a is 10
 b is 20
ob1 after changing ob1:
 a is -1
 b is -1
ob2 after changing ob1:
 a is 10
 b is 20

Using an empty initializer

<source lang="cpp">template<typename T> struct MyType {

 MyType() : value_(T()) {
 }
 explicit MyType(const T& v) : value_(v) {
 }

private:

 T value_;

}; enum color { black, red, green, blue }; struct point {

   int x, y; 

}; int main() {

 MyType<int> i;    
 MyType<color> c;  
 MyType<bool> b;   
 MyType<point> p;  

}</source>