c構造函數和析構函數_C ++構造函數和析構函數| 查找輸出程序| 套裝2

c構造函數和析構函數

Program 1:

程序1:

#include<iostream>
using namespace std;
class Sample
{
private:
int X;
int Y;
public:
Sample(int x, int y)
{
X = x;
Y = y;
}
void set(int x, int y)
{
X = x;
Y = y;
}
void print()
{
cout<<X<<" "<<Y<<endl;
}
};
int main()
{		
Sample S[2];
Sample *PTR;
PTR = S;
PTR[0].set(10,20);
PTR[1].set(30,40);
PTR[0].print();
PTR[1].print();
return 0;
}

Output:

輸出:

main.cpp: In function ‘int main()’:
main.cpp:32:12: error: no matching function for call to ‘Sample::Sample()’
Sample S[2];

Explanation:

說明:

The above code will generate a syntax error because in the main() function we created an array of objects that must be instantiated by the default constructor, but in the class, the default constructor is not defined.

上面的代碼將產生語法錯誤,因為在main()函數中,我們創建了必須由默認構造函數實例化的對象數組 ,但是在類中,未定義默認構造函數。

Program 2:

程式2:

#include <iostream>
using namespace std;
class Sample {
private:
int X;
int Y;
public:
Sample(int x, int y)
{
X = x;
Y = y;
}
void set(int x, int y)
{
X = x;
Y = y;
}
void print()
{
cout << X << " " << Y << endl;
}
};
int main()
{
Sample S[2] = { Sample(0, 0), Sample(0, 0) };
Sample* PTR;
PTR = S;
PTR[0].set(10, 20);
PTR[1].set(30, 40);
PTR[0].print();
PTR[1].print();
return 0;
}

Output:

輸出:

10 20
30 40

Explanation:

說明:

In the above program, we created a class Sample that contains two data members X and Y, one parameterized constructor, set() and print() member functions.?

在上面的程序中,我們創建了一個Sample類,其中包含兩個數據成員 XY ,一個參數化的構造函數 , set()print()成員函數。

Now coming to the main() function, we created an array of objects and that will instantiated by the parameterized constructor. And created a pointer that is being initialized with the base address of array objects. And then called set() and print() functions using the pointer.

現在進入main()函數,我們創建了一個對象數組,該數組將由參數化構造函數實例化。 并創建了一個使用數組對象的基地址初始化的指針。 然后使用指針調用set()print()函數。

Program 3:

程式3:

#include <iostream>
using namespace std;
class Sample {
private:
int X;
public:
Sample() const
{
}
void set(int x)
{
X = x;
}
void print()
{
cout << X << endl;
}
};
int main()
{
Sample S;
S.set(10);
S.print();
return 0;
}

Output:

輸出:

main.cpp:9:14: error: constructors may not be cv-qualified
Sample() const
^~~~~

Explanation:

說明:

The above program will generate an error because we cannot create a constructor as a const member function in C++.

上面的程序將產生錯誤,因為我們不能在C ++中將構造函數創建為const成員函數。

Recommended posts

推薦的帖子

  • C++ Constructor and Destructor | Find output programs | Set 1

    C ++構造函數和析構函數| 查找輸出程序| 套裝1

  • C++ Constructor and Destructor | Find output programs | Set 3

    C ++構造函數和析構函數| 查找輸出程序| 套裝3

  • C++ Constructor and Destructor | Find output programs | Set 4

    C ++構造函數和析構函數| 查找輸出程序| 套裝4

  • C++ Constructor and Destructor | Find output programs | Set 5

    C ++構造函數和析構函數| 查找輸出程序| 套裝5

  • C++ Default Argument | Find output programs | Set 1

    C ++默認參數| 查找輸出程序| 套裝1

  • C++ Default Argument | Find output programs | Set 2

    C ++默認參數| 查找輸出程序| 套裝2

  • C++ Arrays | Find output programs | Set 1

    C ++數組| 查找輸出程序| 套裝1

  • C++ Arrays | Find output programs | Set 2

    C ++數組| 查找輸出程序| 套裝2

  • C++ Arrays | Find output programs | Set 3

    C ++數組| 查找輸出程序| 套裝3

  • C++ Arrays | Find output programs | Set 4

    C ++數組| 查找輸出程序| 套裝4

  • C++ Arrays | Find output programs | Set 5

    C ++數組| 查找輸出程序| 套裝5

  • C++ Class and Objects | Find output programs | Set 1

    C ++類和對象| 查找輸出程序| 套裝1

  • C++ Class and Objects | Find output programs | Set 2

    C ++類和對象| 查找輸出程序| 套裝2

  • C++ Class and Objects | Find output programs | Set 3

    C ++類和對象| 查找輸出程序| 套裝3

  • C++ Class and Objects | Find output programs | Set 4

    C ++類和對象| 查找輸出程序| 套裝4

  • C++ Class and Objects | Find output programs | Set 5

    C ++類和對象| 查找輸出程序| 套裝5

翻譯自: https://www.includehelp.com/cpp-tutorial/constructor-and-destructor-find-output-programs-set-2.aspx

c構造函數和析構函數

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542627.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542627.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542627.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python map函數的作用_Python的map函數

map()是 Python 內置的高階函數&#xff0c;它接收一個函數 f 和一個 list&#xff0c;并通過把函數 f 依次作用在 list 的每個元素上&#xff0c;得到一個新的 list 并返回。 例如&#xff0c;對于list [1, 2, 3, 4, 5, 6, 7, 8, 9] 如果希望把list的每個元素都作平方&#xf…

idea java no sdk_java - intelliJ IDEA 13錯誤:請選擇Android SDK

java - intelliJ IDEA 13錯誤&#xff1a;請選擇Android SDK我已經在Error: Cannot find any configured Android SDK上安裝了intelliJ 12.1.2和Error: Cannot find any configured Android SDK。現在我使用intelliJ 13.0.1創建了一個空的android應用程序項目(也使用模擬器作為…

該怎樣在KeyShot中進行貼圖

2019獨角獸企業重金招聘Python工程師標準>>> 在渲染物體的時候&#xff0c;貼圖常常是不可缺少的部分&#xff0c;貼圖主要用于描述對象表面的物質形態&#xff0c;構造真實世界中自然物質表面的視覺表象。不同的貼圖能給人帶來不同的視覺感受&#xff0c;KeyShot3D…

python淘寶cookies搶購_Python實現淘寶秒殺聚劃算搶購自動提醒源碼

說明 本實例能夠監控聚劃算的搶購按鈕&#xff0c;在聚劃算整點聚的時間到達時發出提醒&#xff08;音頻文件自己定義位置&#xff09;并自動彈開頁面&#xff08;URL自己定義&#xff09;。 同時還可以通過命令行參數自定義刷新間隔時間&#xff08;默認0.1s&#xff09;和監控…

kadane算法_使用KADANE的算法求最大子陣列和

kadane算法What is a sub-array? 什么是子陣列&#xff1f; A sub-array is basically an arrays contiguous part. For example, if we have an array of integers [1,2,3] so the sub-arrays that we can form from the given array are [1], [2] , [3] , [1,2] , [2,3] , …

java汽車油耗計算_轉發一個手機油耗計算器 (java)

今天在一個汽車論壇上看見的&#xff0c;試了試&#xff0c;還真不錯。比以前那個Fuel Consumption 功能要強大,雖然都是JAVA軟件。小羅盤手機計算器是作者獨自產品策劃、美術設計、程序開發、測試發布的手機應用軟件&#xff0c;是為作者的一個朋友寫的&#xff0c;當然我們用…

6.dubbo常用的xml配置有哪些_【面試篇】必須掌握的Spring 常用注解

閱讀文本大概需要5分鐘。注解本身沒有功能的&#xff0c;就和 xml 一樣。注解和 xml 都是一種元數據&#xff0c;元數據即解釋數據的數據&#xff0c;這就是所謂配置。本文主要羅列 Spring|Spring MVC相關注解的簡介。Spring部分1、聲明bean的注解Component 組件&#xff0c;沒…

Linux的iptables常用配置范例(2)

iptables -F #清除所有規則 iptables -X #清除所有自定義規則 iptables -Z #各項計數歸零 iptables -P INPUT DROP #將input鏈默認規則設置為丟棄 iptables -P OUTPUT DROP #將output鏈默認規則設置為丟棄 iptables -A INPUT -i lo -j ACCEPT iptables -A OUTPUT -o lo …

aptitude 命令_C-命令行參數Aptitude問題與解答

aptitude 命令C programming Command Line Arguments Aptitude Questions and Answers: In this section you will find C Aptitude Questions and Answers on Command Line Arguments – Passing values with running programs, separate argument values, number of argument…

文件上傳java邏輯_Java?文件上傳?實例

import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;public class Upload {private String saveDir "."; // 要保存文件的路徑private String contentType ""; // 文檔類型private String charset "";…

matlab數值計算pdf_Gnuplot科學繪圖(九)——柵格以及方程數值解估算

Gnuplot科學繪圖系列內容Gnuplot科學繪圖(一)——從安裝到簡單函數繪圖(文末有彩蛋)Gnuplot科學繪圖(二)——坐標取值范圍及刻度(文末有彩蛋)Gnuplot科學繪圖(三)——點線風格Gnuplot科學繪圖(四)——多組數據繪圖Gnuplot科學繪圖(五)——輸出eps 圖片Gnuplot科學繪圖(六)——輸…

dda算法_計算機圖形學中的DDA(數字差分分析儀)算法

dda算法DDA(數字差分分析儀)算法 (DDA (Digital Differential Analyzer) Algorithm) In computer graphics, the DDA algorithm is the simplest algorithm among all other line generation algorithms. Here, the DDA is an abbreviation that stands for "Digital Diff…

購物商城框架java_基于jsp的購物商城-JavaEE實現購物商城 - java項目源碼

基于jspservletpojomysql實現一個javaee/javaweb的購物商城, 該項目可用各類java課程設計大作業中, 購物商城的系統架構分為前后臺兩部分, 最終實現在線上進行購物商城各項功能,實現了諸如用戶管理, 登錄注冊, 權限管理等功能, 并實現對各類購物商城相關的實體進行管理。該購物…

c語言++數組名【數字】_C ++程序在數組中打印所有非重復數字

c語言數組名【數字】Problem statement: Write a C program to print all the non-repeated numbers in an array in minimum time complexity. 問題陳述&#xff1a;編寫一個C 程序&#xff0c; 以最小的時間復雜度將所有未重復的數字打印在數組中 。 Input Example: 輸入示例…

java最接近對點及距離_最接近點對問題_分治法

一、問題描述給定平面上的n個點&#xff0c;找其中的一對點&#xff0c;使得在n個點組成的所有點對中該點對間的距離最小。二、解題思路及所選算法策略的可行性分析思路&#xff1a;利用分治法來解決問題。遞歸子結構求最接近點對總體可分為幾個步驟&#xff1a;1、當問題規模小…

python return用法_初學Python要了解什么 裝飾器知識匯總有哪些

初學Python要了解什么&#xff1f;裝飾器知識匯總有哪些&#xff1f;在Python學習過程中&#xff0c;有多種方法對函數和類進行加工&#xff0c;相對于其它方式&#xff0c;裝飾器語法簡單&#xff0c;代碼可讀性高。因此&#xff0c;裝飾器在Python項目中有廣泛的應用&#xf…

android emulator虛擬設備分析第三篇之pipe上的qemud service

一、概述 本篇和第二篇是強相關的&#xff0c;需要結合第二篇一起看。 以boot-properties為例&#xff0c;注意不需要看ANDROID-QEMUD.TXT&#xff0c;這個是和guest os中的qemud進行相關的&#xff0c;已廢棄。 啟動emulator時&#xff0c;有一個參數-prop <key><val…

c#異常處理_C#異常處理能力問題和解答 套裝4

c#異常處理1) Which is not a valid keyword used in the context of exception handling? trycatchfinalfinally Answer & Explanation Correct answer: 3final The final keyword is not used to handle exceptions in C#.NET. 1)在異常處理的上下文中使用哪個無效關鍵字…

Castor xsd生成java_java – Castor可以處理從基礎XSD導入的多個XSD生成類嗎?

注意&#xff1a;我是EclipseLink JAXB (MOXy)領導者,也是JAXB 2 (JSR-222)專家組的成員.Can Castor do this? If so, what would be the Ant task syntax for it.If not, would perhaps JAXB be a better alternative?下面是如何使用JAXB完成此操作的示例&#xff1a;產品xm…

串口通信 校驗碼_一文讀懂S7-200 SMART自由口通信!

學習S7-200 SMART時了解到&#xff0c;基于RS485接口可實現一下幾種通信&#xff1a;1&#xff09;modbus RTU通信2&#xff09;PPI協議通信3&#xff09;USS協議通信4&#xff09;自由口通信何為自由口通信呢&#xff1f;前三種通信必須要PLC和與其通信的設備支持相同的通信協…