c ++查找字符串_C ++類和對象| 查找輸出程序| 套裝4

c ++查找字符串

Program 1:

程序1:

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

Output:

輸出:

11

Explanation:

說明:

In the above program, we created class Sample that contain member X and a constant pointer PTR that contains the address of X, here we cannot relocate the pointer but we can change the value of X using pointer PTR.

在上面的程序,我們創建的類樣品含有成員X和包含X的地址,在這里我們不能搬遷的指針,但我們可以用指針PTR改變X的值恒定的指針PTR。

Here, we defined two member functions set() and print() outside the class.

在這里,我們在類外部定義了兩個成員函數set()print()

The set() function set the value to the data member X. and print() member function uses cout statement.

set()函數將值設置為數據成員X。print()成員函數使用cout語句。

cout << *PTR-EOF << " ";

The value of EOF is -1 then 10- -1 = 11.

EOF的值為-1,然后10- -1 = 11

Thus, 11 will be printed on the console screen.

因此,將在控制臺屏幕上打印11

Program 2:

程式2:

#include <iostream>
using namespace std;
class Sample {
int X;
int* const PTR = &X;
public:
void set(int x);
void print();
};
void Sample::set(int x)
{
*PTR = x;
}
void Sample::print()
{
cout << *PTR - EOF << " ";
}
int main()
{
Sample* S;
S->set(10);
S->print();
return 0;
}

Output:

輸出:

Segmentation fault (core dumped)
OR
Runtime error

Explanation:

說明:

The above code will generate a runtime error because in the above program we created the pointer of the class but, we did not initialize pointer PTR with any object of the Sample class. If we try to access the member of the class Sample then it will generate a runtime error.

上面的代碼將產生運行時錯誤,因為在上面的程序中我們創建了該類的指針,但沒有使用Sample類的任何對象初始化指針PTR 。 如果我們嘗試訪問Sample類的成員,則它將生成運行時錯誤

Program 3:

程式3:

#include <iostream>
using namespace std;
class Sample {
int X;
int* const PTR = &X;
public:
void set(int x);
void print();
};
void Sample::set(int x)
{
*PTR = x;
}
void Sample::print()
{
cout << *PTR - EOF << " ";
}
int main()
{
Sample* S = &(Sample());
S->set(10);
S->print();
return 0;
}

Output:

輸出:

main.cpp: In function ‘int main()’:
main.cpp:25:27: error: taking address of temporary [-fpermissive]
Sample* S = &(Sample());
^

Explanation:

說明:

The above program will generate an error due to the below statement used in the main() function:

由于main()函數中使用了以下語句,因此上述程序將生成錯誤:

Sample *S = &(Sample());

Here we created a pointer of the class and tried to initialize with the object of the class, this is not the correct way. The correct way is,

這里我們創建了一個類的指針,并試圖用該類的對象進行初始化,這不是正確的方法。 正確的方法是

Sample OB;
Sample *S = &OB;

Recommended posts

推薦的帖子

  • 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 5

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

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

    C ++循環| 查找輸出程序| 套裝1

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

    C ++循環| 查找輸出程序| 套裝2

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

    C ++循環| 查找輸出程序| 套裝3

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

    C ++循環| 查找輸出程序| 套裝4

  • C++ Looping | 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

翻譯自: https://www.includehelp.com/cpp-tutorial/class-and-objects-find-output-programs-set-4.aspx

c ++查找字符串

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

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

相關文章

ASP.NET 泛型類型 Dictionary操作

protected void Page_Load(object sender, EventArgs e){//泛型Dictionary Dictionary<string, string> dit new Dictionary<string, string>();dit.Add("13", "張三");dit.Add("22", "李四");Response.Write("總數…

獨立看門狗---STM32----HAL

基本概念 看門狗解決的問題是什么&#xff1f; 在系統跑飛&#xff08;程序異常執行&#xff09;的情況&#xff0c;是系統復位&#xff0c;程序重新執行。 獨立看門狗適應用于需要看門狗作為一個在主程序之外能夠完全獨立工作&#xff0c;并且對時間精度要求低的場合。 工…

實訓09.09:簡單的彩票系統(注冊信息)

package wsq; import java.util.Scanner;//本文件負責注冊用戶信息 /*用戶注冊信息:1.要求設置賬號和密碼,使用字符串數組2.賬號名不能重復3.密碼需要輸入兩次,兩次密碼輸入一致4.滿足賬號名不重復.且兩次密碼一致,即為注冊成功!!將信息添加到字符串數組中String[][] users ne…

【轉】JAVA生成縮略圖

方法1&#xff1a;[第一種方法比后一種生成的縮略圖要清晰] import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.WritableRaster;import java.awt.*;import java.awt.geom.AffineTransform;import jav…

javascript寫入_如何在JavaScript中寫入HTML元素?

javascript寫入寫入HTML元素 (Writing into an HTML element) To write string/text into an HTML element, we use the following things: 要將字符串/文本寫入HTML元素&#xff0c;我們使用以下內容&#xff1a; There must be an HTML element like paragraph, span, div e…

大話設計模式之設計模式遵循的七大原則

最近幾年來&#xff0c;人們踴躍的提倡和使用設計模式&#xff0c;其根本原因就是為了實現代碼的復用性&#xff0c;增加代碼的可維護性。設計模式的實現遵循了一些原則&#xff0c;從而達到代碼的復用性及增加可維護性的目的&#xff0c;設計模式對理解面向對象的三大特征有很…

IIC通信---EEPROM24C02---STMF4

IIC通信協議 IIC是同步半雙工通信&#xff0c;一個數據線SDA和一個時鐘SCL線&#xff0c;可以接受和發送數據。在CPU與被控IC之間、IC與IC之間進行雙向傳送。 空閑狀態 IIC總線的SDA和SCL兩條信號線同時處于高電平時&#xff0c;規定為總線的空閑狀態。 起始信號 當SCL為高…

實訓09.08:簡單的算法練習

/*final 關鍵字 修飾的變量值 后期不可更改 相當于定義常量常量 &#xff1a;不可更改*/final int a 10;//a 20; 報錯的值不可更改&#xff01;/*輸入函數* */System.out.println("請輸入數字&#xff1a;");Scanner scanner new Scanner(System.in);int b…

讓自己閃亮

轉載于:https://www.cnblogs.com/Gigabyte/archive/2009/01/03/you_can_shine.html

Java中的wait()和sleep()方法之間的區別

Java中的wait()和sleep()方法 (wait() and sleep() methods in Java) First, we will see how wait() method differs from sleep() method in Java? 首先&#xff0c;我們將看到wait()方法與Java中的sleep()方法有何不同&#xff1f; wait()方法 (wait() Method) This metho…

離線使用iPhone SDK文檔的方法

在使用Xcode進行iPhone編程時&#xff0c;有時需要參考iPhone SDK的文檔&#xff0c;不過每次ControlClick后&#xff0c;Xcode都會試圖連接Internet&#xff0c;進行在線讀取。有什么方法能夠把資料下載到硬盤上進行離線閱讀嗎&#xff1f; 答案是肯定的。首先去Xcode的Prefer…

遠程連接sql server 2000服務器的解決方案

遠程連接sql server 2000服務器的解決方案2007-04-07 11:29遠程連接sql server 2000服務器的解決方案   一 看ping 服務器IP能否ping通。   這個實際上是看和遠程sql server 2000服務器的物理連接是否存在。如果不行&#xff0c;請檢查網絡&#xff0c;查看配置&#xff0c…

實訓09.10:HTML簡單表格設計

<!DOCTYPE html> <html><head><meta charset"UTF-8"><title>燕雨簡歷</title></head><body><table border"" cellspacing"" cellpadding"" width"400px" height"6…

LCD顯示實驗----STM32f4--HAL

步驟 LCD初始化 LCD_Init(); //LCD初始化此函數在lcd.c文件里面 2. 設置LCD背景顏色 LCD_Clear(WHITE);此函數在lcd.c文件里面 3. 設置字體顏色 POINT_COLORRED; 寫入要顯示的字體 LCD_ShowString(10,80,240,24,24,"LTDC TEST");LCD_ShowSt…

JavaScript | 使用提示從用戶輸入值

Example 1) Input name and print 示例1)輸入名稱和打印 Code (JS & HTML): 代碼(JS和HTML)&#xff1a; <!DOCTYPE html><HTML><HEAD><SCRIPT>var name prompt("Enter Your name:");var msg "Welcome "name;//alert(msg)…

一個游戲程序員的學習資料 (zz)

一個游戲程序員的學習資料//z 2012-4-19 14:39:51 PM IS2120CSDN想起寫這篇文章是在看侯杰先生的《深入淺出MFC》時, 突然覺得自己在大學這幾年關于游戲編程方面還算是有些心得&#xff0c;因此寫出這篇小文,介紹我眼中的游戲程序 員的書單與源代碼參考。一則是作為自己今后兩年…

項目管理中工作分解結構模型(WBSM)的應用

摘要 本文根據工作分解結構(WBS)的工作特點&#xff0c;運用系統工程的思想理論方法&#xff0c;構建了工作分解結構模型&#xff0c;并提出了模型算法;該模型方法的建立使得WBS工作更加簡單可靠、思路清晰、基于更加可靠的科學基礎之上。 1、工作分解結構模型(WBSM)方法工作程…

實訓09.11:java重點內容介紹

package test;/** * OP:面向對象的簡稱* 類&#xff1a;同一特征的屬性* * 類的定義&#xff1a;具有相同特征和行為的事物的抽象。&#xff08;不具體化&#xff09;* 對象&#xff08;實例對象&#xff09;&#xff1a;具體真實存在的實例。* 類是對象的實例&#xff1a;* *…

SPI通信原理---STM32F4--HAL

SPI接口原理 SPI是一種高速全雙工同步通信&#xff0c;在芯片管腳上占用四根線&#xff0c;主要應用在EEPROM、FLASH、實時時鐘、AD轉換器&#xff0c;還有數字信號處理器和數字信號解碼器之間。 SPI接口使用4根線通信。 MISO&#xff1a;主設備數據輸入&#xff0c;從設備數…

Linux 引導管理器 grub2 使用簡介

轉自&#xff1a;杜昌彬的空間 首先向其致敬&#xff01;有改動。 grub是Linux系統即其他類unix系統的主流bootloder&#xff0c;由于grub原來版本的設計存在很大缺陷&#xff0c;與以前的grub很不相同&#xff0c;其使用和配置也發生很大變化。現在很多Linux發行版本都使用了…