C++ basic to class type

C++ basic to class type

The conversion form basic type to class type is easy to accomplish. It may be recalled that the use of instructors was illustrated in a number of examples to initialize objects. For example, a constructor was used to build a vector object from an int data type array. Similarly, we used another constructor to build a string type object from a char* type variable. here are all examples where constructors perform a type conversion from the argument’s type to the constructor’s class type.

Consider the following constructor:

String : : string(char *a)
{
length = strlen(a);
p = new char[length+1];
strcpy(p,a);
}

This constructors builds a string type object from a char* type variable a as show in the above example. The variables length and p are data members of the string class. Once this constructor has been defined in the class string , It can be used for conversation from char* type to string type.

Example:
String s1,s2;
char? name1 = “IBM PC”;
char? name2 = “Apple Computers”;
s1 = string(name1);
s2 = name2;

The statement
S1 = string(name);
S2 = name2;

Also does the same job by invoking the constructor implicitly. Let us take another example of converting an int data type to a class type.

Class time
{
int hrs;
int mins;
public;
. . . .
. . . .
time(int t) // constructor
{
hours = t/60; // t in minutes
mins = t%60;
}
};

The following conversion statements can be used in a function:

Time = T1; // object T1 created
int duration = 85;
T1 = duration; // int to class type

After this conversion, the hours member of T1 object will contain a value of 1 and mins member a value of 25, denoting 1 hours and 25 minutes.

Note ! The constructors are used for type conversion, take the single argument whose type is to be converted.

In both the examples, the left-hand side operand of = operator is always be a class object, Therefore, we can also accomplish this conversion using an
overloaded = operator.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise