C++: An instance of an object

This entry is part 22 of 61 in the series C++

It is easy for us to understand that an object is an entity.

When it comes to an object-oriented program, the emphasis now is on the object.  In a procedural programming such as PASCAL or Fortran, we would exchange data using variables.

Since an object is an entity, can we pass an object from one object to another?

Well, can can do that using an instance of the object.  Let’s consider the example below.

#include <iostream>
#include <string>

using namespace std;

class Country
{

public:
	string name;
	int noAlbum;
	string latest;

};

int main()
{

	Country ts; // instance declaration

	ts.name = "Taylor Swift"; // member initialization
	ts.noAlbum = 5;
	ts.latest = "1986";

	cout << "\n Country Singer = " << ts.name;
	cout << "\n No of Album = " << ts.noAlbum;
	cout << "\n Her latest album = " << ts.latest;
	cout << "\n";

	Country ts1;

	ts1 = ts;

	cout << "\n Country Singer = " << ts1.name;
	cout << "\n No of Album = " << ts1.noAlbum;
	cout << "\n Her latest album = " << ts1.latest;
	cout << "\n";

	return 0;
}

 

In the example above, we have used the first instance ts to hold the first set of data.  ts is an object in itself but in OOP, we would call it an instance of the class Country.

Now after we have created another instance of the Class Country ts1, we are able to use an assignment operator = to assign an object from ts to ts1.

Notice when we print out the data members of ts1, it has the same set of data as ts.instance

WPF: Using instance name for ListBox

This entry is part 34 of 54 in the series Learn WPF

In the previous example, we have used static resource for the data binding.

There is another method where we could display the data in the ListBox.  This is by defining an instance name for the ListBox.

In the example below, I have given the Name of the ListBox as lb.  In the code behind, we can add the properties such as the Name, Address to a list.  We need to use similar instance name lb for the list so that they could be mapped correctly.

MainWindow.xaml
<Window x:Class="ListBoxNSpace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Name="lb" Width="350" Margin="0,5,0,10">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Padding="5,0,5,0" Text="{Binding FirstName}" />
                        <TextBlock Text="{Binding LastName}" />
                        <TextBlock Text=", " />
                        <TextBlock Text="{Binding Address}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

</Window>
MainWindow.xaml.cs
using System;
using System.Collections.ObjectModel;
using System.Windows;

namespace ListBoxNSpace
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
 
    public partial class MainWindow : Window
    {

        public class Customer
        {
            public String FirstName { get; set; }
            public String LastName { get; set; }
            public String Address { get; set; }

            public Customer(String firstName, String lastName, String address)
            {
                this.FirstName = firstName;
                this.LastName = lastName;
                this.Address = address;
            }
        }

        public MainWindow()
        {
            InitializeComponent();

            List<Customer> cust = new List<Customer>();

            cust.Add(new Customer("Michael", "Anderberg", "12 North Third Street"));
            cust.Add(new Customer("Chris", "Ashton", "34 West Fifth Street"));
            cust.Add(new Customer("Cassie", "Hicks", "56 East Seventh Street"));
            cust.Add(new Customer("Guido", "Pi", "78 South Ninth Street"));

            lb.ItemsSource = cust;
          
        }
    }
}

instancelistbox

 

 

 

 

WPF: Defining an instance in XAML

This entry is part 30 of 54 in the series Learn WPF

In WPF, we can actually create an instance of a Class such as the PopStar in the example below by defining it in the XML namespace.

<Window x:Class="CustType.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CustType"
        Title="MainWindow" Height="350" Width="525">
    <Label FontSize="20">
        <local:PopStar Name="Pitbull" 
             Album="Wild Wild Love" Price="12" 
               YearReleased="2013" />
    </Label>
</Window>

Notice that line 7 is actually the content of the Label control.

In line 7, we are actually creating an instance of a class called PopStar.  We have local:PopStar and so when the XAML is compiled, it will look for a class defined in clr-namespace:Custtype.

This actually corresponds to CustType namespace in MainWindow.xaml.cs.

using System.Windows;

namespace CustType
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    class PopStar
    {
        public string Name { get; set; }
        public string Album { get; set; }
        public decimal Price { get; set; }
        public int YearReleased { get; set; }


        public override string ToString()
        {
            return string.Format("{0} by {1}\nReleased {2}", Album, Name, YearReleased);
        }

    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

The output from running the above code is as below.  The output is actually formatted by line 32 by overriding the ToString method.

instanceXAML

To illustrate the example better, I can also add another instance in the XAML.

<Window x:Class="CustType.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CustType"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
     <Label FontSize="20">
        <local:PopStar Name="Pitbull" 
             Album="Wild Wild Love" Price="12" 
               YearReleased="2013" />
     </Label>
     <Label FontSize="20">
        <local:PopStar Name="Madonna" 
         Album="Immaculate Collection" Price="10" 
           YearReleased="1991" />
     </Label>
    </StackPanel>
</Window>

The output from running the above code will be:

instance2

 

 

C#: Assigning an instance

This entry is part 20 of 71 in the series C#

When we create an instance, some also called it instantiate an object, we can assign the instance to another instance.

For example, we create an instance using new operator:

Friend friend = new Friend();

We can assign the friend instance to a different instance name call pal.

using System;

namespace Hello
{
    class Friend
    {
        private string name;
        private string major;

        private Friend()
        {
            name = "John";
            major = "Biology";
        }

        static void Main()
        {
            Friend friend = new Friend();

            Console.WriteLine(friend.name);
            Console.WriteLine(friend.major);
            Console.WriteLine();  //This adds an empty new line

            Friend pal = friend;

            Console.WriteLine("How about pal?");
            Console.WriteLine(pal.name);
            Console.WriteLine(pal.major);

            Console.ReadLine();
        }
    }
}

The line

Friend pal = friend

copies the value of friend to pal.

As a result, pal is now referencing the same Friend object as friend.

On the other hand, the following code creates two different Friend objects:

Friend friend = new Friend();
Friend pal = new Friend();

pal

PHP & OOP: Instance of a class

This entry is part 4 of 11 in the series PHP & OOP

The keyword new is to create an instance of a class.

<?php
class SimpleClass
{
    public $var = 'a default value';

    public function displayVar() {
        echo $this->var;
    }
}

//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayVar();

?>

As in the line

$simple_object = new SimpleClass();

You can also think of it as creating an object from the class SimpleClass.  With the instance $simple_object, we can access the class variable using

$simple_object->displayVar()

In PHP, we have used -> operator.  In other programming languages such as C++, we would have used the dot(.) operator.