Passing dan Return Object dari Function di Pemrograman C++

Passing dan Return Object dari Function di Pemrograman C++

Selamat datang kembali di TeknoTips.NET. TeknoTips.NET adalah media online Indonesia yang diluncurkan pada bulan Maret 2015. Kami berkomitmen menyajikan konten seputar teknologi yang berkualitas dan bermanfaat. Ulasan kami terdiri dari berbagai topik: pemrograman komputer & database, review produk, tutorial windows, linux, android, kumpulan source code & project program, referensi tugas akhir & skripsi, download ebook, game & software.

Pada kesempatan ini kita akan mengulas artikel yang berjudul : Passing dan Return Object dari Function di Pemrograman C++.

In C++ programming, objects can be passed to function in similar way as variables and structures.

Procedure to Pass Object to Function

Tag : C++ Objects and Class, C++ Constructors, C++ Objects & Function, C++ Operator Overloading.

Example to Pass Object to Function

C++ program to add two complex numbers by passing objects to function.

#include <iostream>
using namespace std;
class Complex
{
    private:
       int real;
       int imag;
    public:
       Complex(): real(0), imag(0) { }
       void Read()
        {
           cout<<"Enter real and imaginary number respectively:"<<endl;
           cin>>real>>imag;
        }
        void Add(Complex comp1,Complex comp2)
        {
            real=comp1.real+comp2.real;
 /* Here, real represents the real data of object c3 because */
/* this function is called using code c3.add(c1,c2); */
            imag=comp1.imag+comp2.imag;
 /* Here, imag represents the imag data of object c3 because */
/* this function is called using code c3.add(c1,c2); */
        }
        void Display()
        {
            cout<<"Sum="<<real<<"+"<<imag<<"i";
        }
};
int main()
{
    Complex c1,c2,c3;
    c1.Read();
    c2.Read();
    c3.Add(c1,c2);
    c3.Display();
    return 0;
}

Output

Enter real and imaginary number respectively:
12
3
Enter real and imaginary number respectively:
2
6
Sum=14+9i

 

Returning Object from Function

The syntax and procedure to return object is similar to that of returning structure from function.
Tag : C++ Objects and Class, C++ Constructors, C++ Objects & Function, C++ Operator Overloading.

 

Example to Return Object from Function

This program is the modification of above program displays exactly same output as above. But, in this program, object is return from function to perform this task.

#include <iostream>
using namespace std;
class Complex
{
    private:
       int real;
       int imag;
    public:
       Complex(): real(0), imag(0) { }
       void Read()
        {
           cout<<"Enter real and imaginary number respectively:"<<endl;
           cin>>real>>imag;
        }
        Complex Add(Complex comp2)
        {
            Complex temp;
            temp.real=real+comp2.real;
/* Here, real represents the real data of object c1 */
/* because this function is called using code c1.Add(c2) */
            temp.imag=imag+comp2.imag;
/* Here, imag represents the imag data of object c1 */
/* because this function is called using code c1.Add(c2) */
            return temp;
        }
        void Display()
        {
            cout<<"Sum="<<real<<"+"<<imag<<"i";
        }
};
int main()
{
    Complex c1,c2,c3;
    c1.Read();
    c2.Read();
    c3=c1.Add(c2);
    c3.Display();
    return 0;
}
 

Tag : Tag : C++ Objects and Class, C++ Constructors, C++ Objects & Function, C++ Operator Overloading. 
Untuk mendapat notifikasi setiap artikel terbaru, masukkan e-mail anda disini
Selanjutnya cek e-mail anda untuk verifikasi.

Silahkan tuliskan komentar relevan Anda dengan jelas dan sopan, sesuai dengan topik postingan tentang ", " pada halaman dibawah ini.

0 Response to "Passing dan Return Object dari Function di Pemrograman C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2


Iklan Bawah Artikel