Tổng hợp và thành phần trong c ++

Trong lập trình hướng đối tượng, các lớp tương tác với nhau để thực hiện một hoặc nhiều tính năng của ứng dụng. Bạn có thể xác định mối quan hệ giữa các lớp trong khi thiết kế các lớp cho ứng dụng của mình

Có ba loại quan hệ trong lập trình hướng đối tượng dựa trên cách một lớp tương tác với lớp khác

  1. Sự kết hợp
  2. Thành phần
    • Thành phần
    • tổng hợp
  3. Di sản

Hình dưới đây minh họa các mối quan hệ

Tổng hợp và thành phần trong c ++
Mối quan hệ lớp học

Sự kết hợp

Mối quan hệ kết hợp được gọi là mối quan hệ "sử dụng một" trong đó một lớp sử dụng một lớp khác để thực hiện một số thao tác. Trong sự liên kết, cả hai lớp có thể tồn tại độc lập khi không ai là chủ sở hữu của lớp khác. Một số người coi hiệp hội là sự hợp tác hoặc ủy quyền

Sự liên kết xảy ra giữa các lớp trong đó một lớp cung cấp dịch vụ cho lớp khác hoặc lớp ủy thác một số loại hành vi cho lớp khác

Liên kết thường được triển khai với một biến thể hiện con trỏ hoặc tham chiếu hoặc dưới dạng đối số phương thức

Ví dụ sau minh họa mối quan hệ kết hợp giữa lớp

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 và lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
8

Thí dụ. Sự kết hợp

Sao chép

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
}

public class StudentRepository
{
    public Student GetStudent(int StudentId)
    {
        // get student by id from db here

        return new Student();
    }
    public bool Save(Student student)
    {
        // save student to db here
        Console.WriteLine("Student saved successfully");

        return true;
    }
    public bool Validate(Student student)
    {
        // get student from db to check whether the data is already exist
        Console.WriteLine("Student does not exist.");

        return true;
    }
}

Trong ví dụ trên, lớp

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
8 sử dụng lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 để lưu hoặc truy xuất dữ liệu sinh viên. Lưu ý rằng lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
8 sử dụng lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 làm tham số của các phương thức. Lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
8 cung cấp dịch vụ cho bất kỳ lớp nào quan tâm đến việc lưu hoặc truy xuất dữ liệu học sinh. Tuy nhiên, cả hai lớp đều có thời gian tồn tại độc lập, nghĩa là việc loại bỏ một lớp không loại bỏ lớp khác. Vì vậy, chúng ta có thể nói rằng mối quan hệ giữa lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
8 và lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 là sự liên kết (hoặc hợp tác/ủy nhiệm). Bạn cũng có thể nói rằng lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 giao trách nhiệm thực hiện việc lưu và truy xuất dữ liệu liên quan đến sinh viên cho lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
8

Mối quan hệ liên kết giữa các lớp được đánh dấu bằng mũi tên trong sơ đồ UML, như hình bên dưới

Tổng hợp và thành phần trong c ++

Điểm quan trọng

  • Một lớp chỉ sử dụng các hành vi/chức năng (phương thức) của lớp khác nhưng không thay đổi chúng bằng cách ghi đè chúng
  • Một lớp không kế thừa lớp khác
  • Một lớp không bao gồm (sở hữu) một lớp khác với tư cách là thành viên chung
  • Cả hai lớp đều có thời gian tồn tại độc lập trong đó việc loại bỏ một lớp không tự động loại bỏ lớp khác

Thành phần

Thành phần được gọi là "có một" mối quan hệ. Mối quan hệ thành phần được hình thành khi một lớp có tham chiếu đến lớp khác dưới dạng thuộc tính thể hiện

Trong các mối quan hệ thành phần, một lớp chứa tham chiếu đến một lớp khác là cha (chủ sở hữu) của lớp con đó. Lớp con không có lớp cha thì không tồn tại

Ví dụ: lớp

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 sau đây có mối quan hệ tổng hợp với lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
6 chứa địa chỉ của học sinh. Đối tượng
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
6 không có đối tượng
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 không thể tồn tại

Thí dụ. Thành phần

Sao chép

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}

Trong ví dụ trên, lớp

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 có lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
6 là thuộc tính công khai, điều này tạo nên mối quan hệ tổng hợp giữa lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 và lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
6. Nếu đối tượng
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 bị xóa thì đối tượng
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
6 cũng sẽ bị xóa

Mối quan hệ tổng hợp cũng có một yếu tố cơ bản là mối quan hệ một-một, một-nhiều hoặc nhiều-nhiều giữa các lớp. Trong ví dụ trên, lớp

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 và lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
6 có mối quan hệ một đối một vì mỗi lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 sẽ chỉ có một địa chỉ

Một lớp cũng có thể bao gồm thuộc tính


public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public int AddressId { get; set; }
}
8 của một lớp khác thay vì một thể hiện để hình thành mối quan hệ tổng hợp. Ví dụ: lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 có thể chứa thuộc tính
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Course EnrolledCourse { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public IList Topics { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}
0 trỏ đến lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
6

Thí dụ. Thành phần sử dụng thuộc tính Id

Sao chép

________số 8

Mối quan hệ thành phần giữa các lớp được đánh dấu bằng cách sử dụng dòng trong sơ đồ UML, như hình bên dưới

Tổng hợp và thành phần trong c ++

Điểm quan trọng

  • Một lớp (cha) chứa tham chiếu đến một lớp khác (con)
  • Lớp con không tồn tại nếu không có lớp cha
  • Xóa lớp cha cũng sẽ xóa lớp con
  • Một lớp cũng có thể bao gồm tham chiếu thuộc tính id của lớp khác

tổng hợp

Tập hợp là một thể loại khác của mối quan hệ "có mối quan hệ" trong đó một lớp có thể chứa các lớp khác làm thuộc tính nhưng các lớp đó có thể tồn tại độc lập

Ví dụ: lớp

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 chứa thể hiện của lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Course EnrolledCourse { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public IList Topics { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}
3 làm thuộc tính để hình thành mối quan hệ thành phần. Tuy nhiên, cả hai lớp có thể tồn tại độc lập nên được gọi là quan hệ tập hợp

Thí dụ. tổng hợp

Sao chép

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Course EnrolledCourse { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public IList Topics { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

Trong quan hệ tập hợp trên, ngay cả khi đối tượng

public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 bị xóa thì đối tượng
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Course EnrolledCourse { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public IList Topics { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}
3 vẫn tồn tại. Lớp
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Address HomeAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

}
7 cũng có thể chứa thuộc tính
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Course EnrolledCourse { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public IList Topics { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}
7 thay vì thể hiện của
public class Student
{
    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public Course EnrolledCourse { get; set; }
}

public class Course
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public IList Topics { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}
3

Cả Thành phần và Tập hợp đều có mối quan hệ "có" nhưng trong mối quan hệ thành phần, các lớp liên quan không tồn tại độc lập trong khi, trong tập hợp, các lớp liên quan tồn tại độc lập

Mối quan hệ tổng hợp giữa các lớp được đánh dấu bằng một dòng trong sơ đồ UML, như hình bên dưới

tổng hợp và thành phần là gì?

1. Liên kết giữa hai đối tượng minh họa cho mối quan hệ “có-một” được gọi là Tập hợp. Một thành phần xác định một phần của mối quan hệ và cả hai thực thể được kết nối với nhau. 2

Tổng hợp và thành phần trong OOP là gì?

Tập hợp hàm ý một mối quan hệ trong đó phần tử con có thể tồn tại độc lập với phần tử gốc . Thí dụ. Lớp học (phụ huynh) và Học sinh (con). Xóa Lớp học và Học sinh vẫn tồn tại. Thành phần ngụ ý một mối quan hệ mà đứa trẻ không thể tồn tại độc lập với cha mẹ. Thí dụ. Nhà (cha mẹ) và Phòng (con).

Sự khác biệt giữa tập hợp hiệp hội và thành phần là gì?

Tóm lại, mối quan hệ giữa hai đối tượng được gọi là liên kết và liên kết được gọi là thành phần khi một đối tượng sở hữu đối tượng khác trong khi liên kết được gọi là tập hợp khi một đối tượng . .

một ví dụ về tập hợp là gì?

Tập hợp là một tập hợp hoặc tập hợp nhiều thứ lại với nhau. Bộ sưu tập thẻ bóng chày của bạn có thể đại diện cho sự tổng hợp của rất nhiều loại thẻ khác nhau. Tập hợp xuất phát từ quảng cáo tiếng Latinh, có nghĩa là và gegare, có nghĩa là bầy đàn. Vì vậy, từ này lần đầu tiên được sử dụng theo nghĩa đen là đàn hoặc đàn.